记录编号 116763 评测结果 AAAAAAAAAAAAAA
题目名称 [USACO Feb07] 银色莲花池 最终得分 100
用户昵称 GravatarHouJikan 是否通过 通过
代码语言 C++ 运行时间 0.025 s
提交时间 2014-08-26 21:31:03 内存使用 2.16 MiB
显示代码纯文本
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#include <vector>
#include <ctime>
#include <iterator>
#include <functional>
#define pritnf printf
#define scafn scanf
#define For(i,j,k) for(int i=(j);i<=(k);(i)++)
using namespace std;
typedef long long LL;
typedef unsigned int Uint; 
const int INF=0x7ffffff;
//==============struct declaration==============
struct node
{
  int r,c,add,dist;
};
//==============var declaration=================
int r,c;
int maps[40][40];
int minf[40][40];
int dist[40][40][100];
LL way[40][40][100];
int mvr[]={-1,-2,-2,-1,1,2,2,1};
int mvc[]={-2,-1,1,2,2,1,-1,-2};
//==============function declaration============

//==============main code=======================
int main()
{  
  string FileName="silvlily";//程序名 
  string FloderName="COGS";//文件夹名 
  freopen((FileName+".in").c_str(),"r",stdin);
  freopen((FileName+".out").c_str(),"w",stdout);
#ifdef DEBUG  
  system(("cp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\standard.cpp C:\\Users\\Administrator\\Desktop\\"+FloderName+"\\submit.txt").c_str());
#endif    
  scanf("%d%d",&r,&c);
  node start;
  int endr,endc;
  For(i,1,r)
    For(j,1,c)
    {
      scanf("%d",&maps[i][j]);
      if (maps[i][j]==3)
      {
        start.r=i;
        start.c=j;
        start.add=0;
        start.dist=0;
        maps[i][j]=2;
      }
      if (maps[i][j]==4)
      {
        endr=i;
        endc=j;
        maps[i][j]=1;
      }
    }
  For(i,1,r)
    For(j,1,c)
      minf[i][j]=INF;
  minf[start.r][start.c]=0;
  queue <node> Q;
  Q.push(start);
  int mind=INF;
  //第一次BFS,计算最短时间以及最少莲花 
  while (!Q.empty())
  {
    node now=Q.front();Q.pop();
    if (now.r==endr&&now.c==endc)
    {
      if (now.add==minf[endr][endc])
        if (now.dist<mind)
          mind=now.dist;
      continue;  
    }
    int nr,nc;
    For(i,0,7)
    {
      nr=now.r+mvr[i];
      nc=now.c+mvc[i];
      if (nr>=1&&nr<=r&&nc>=1&&nc<=c)
      {
        if (maps[nr][nc]==2) continue;
        if (maps[nr][nc]==1)
          if (now.add<minf[nr][nc])
          {
             minf[nr][nc]=now.add;
             node addn;
             addn.r=nr;
             addn.c=nc;
             if (nr==endr&&nc==endc)
               mind=INF;
             addn.dist=now.dist+1;
             addn.add=now.add;
             Q.push(addn);
          }
        if (maps[nr][nc]==0)
          if (now.add+1<minf[nr][nc])
          {
             minf[nr][nc]=now.add+1;
             node addn;
             addn.r=nr;
             addn.c=nc;
             addn.dist=now.dist+1;
             addn.add=now.add+1;
             Q.push(addn);
          }
      } 
    }
  }
  if (minf[endr][endc]==INF)
   {
     printf("-1\n");
     return 0;
   } 
  printf("%d\n",minf[endr][endc]);
  //第二次BFS,最短路数目 
  start.dist=0;
   memset(dist,0,sizeof(dist));
  //dist[i][j][k]表示添加k朵莲花到达[i][j]点的最小时间
  For(i,1,r)
    For(j,1,c)
      For(k,0,mind)
        dist[i][j][k]=INF;
  memset(dist[start.r][start.c],0,sizeof(dist[start.r][start.c]));
  memset(way,0,sizeof(way));
  way[start.r][start.c][0]=1;
  Q.push(start);
  while (!Q.empty())
  {
    node now=Q.front();Q.pop();
    if (now.r==endr&&now.c==endc&&now.add<=minf[endr][endc])
        continue;
    int nr,nc;
    For(i,0,7)
    {
      nr=now.r+mvr[i];
      nc=now.c+mvc[i];
      if (nr>=1&&nr<=r&&nc>=1&&nc<=c)
      {
        if (maps[nr][nc]==2) continue;
        if (maps[nr][nc]==1)
        {
          node addn;
          addn.add=now.add;
          addn.dist=now.dist+1;
          addn.r=nr;addn.c=nc;
          if (addn.dist<=mind&&addn.add<=minf[endr][endc])
            if (addn.dist<=dist[nr][nc][addn.add])
            {
              if (addn.dist<dist[nr][nc][addn.add])
              {
                Q.push(addn);
                way[nr][nc][addn.add]=0;
              }
              way[nr][nc][addn.add]+=way[now.r][now.c][now.add];
              dist[nr][nc][addn.add]=addn.dist;
            }
        }
        if (maps[nr][nc]==0)
        {
          node addn;
          addn.add=now.add+1;
          addn.dist=now.dist+1;
          addn.r=nr;addn.c=nc;
          if (addn.dist<=mind&&addn.add<=minf[endr][endc])
            if (addn.dist<=dist[nr][nc][addn.add])
            {
              if (addn.dist<dist[nr][nc][addn.add])
              {
                Q.push(addn);
                way[nr][nc][addn.add]=0;
              }
              way[nr][nc][addn.add]+=way[now.r][now.c][now.add];
              dist[nr][nc][addn.add]=addn.dist;
            }
        }
      }
    }
  }
  printf("%d\n%lld\n",mind,way[endr][endc][minf[endr][endc]]);
  return 0;
}
//================fuction code====================