跳过正文
  1. Posts/

poj 2251 Dungeon Master (三维bfs)

·571 字·2 分钟

http://poj.org/problem?id=2251

简单bfs,只不过是三维的。。。

唯一的坑点在输出上…

Escaped in %d minute(s)

这意思是答案为1输出minute,不为1输出minutes还是说是不是1都输出minute(s)? 试了下,答案是后者。

另:终于找到了好的读地图的方法。。。而不用担心回车符。

就是先读成字符串。

具体见代码

代码实现
  1
  2
  3    /*************************************************************************
  4    	> File Name: code/2015summer/searching/B.cpp
  5    	> Author: 111qqz
  6    	> Email: rkz2013@126.com
  7    	> Created Time: 2015年07月17日 星期五 16时47分46秒
  8     ************************************************************************/
  9
 10    #include<iostream>
 11    #include<iomanip>
 12    #include<cstdio>
 13    #include<algorithm>
 14    #include<cmath>
 15    #include<cstring>
 16    #include<string>
 17    #include<map>
 18    #include<set>
 19    #include<queue>
 20    #include<vector>
 21    #include<stack>
 22    using namespace std;
 23    #define REP(i, n) for (int i=0;i<int(n);++i)
 24    typedef long long LL;
 25    typedef unsigned long long ULL;
 26    const int N=40;
 27    char st[N][N][N];
 28    int d[N][N][N];
 29    int l,r,c;
 30    int sx,sy,sz,tx,ty,tz;
 31    int dirx[6]={1,-1,0,0,0,0};
 32    int diry[6]={0,0,-1,1,0,0};
 33    int dirz[6]={0,0,0,0,1,-1};
 34
 35    bool ok(int x,int y,int z)
 36    {
 37        if (z>=0&&z<l&&x>=0&&x<r&&y>=0&&y<c&&d[z][x][y]==-1&&st[z][x][y]!='#')
 38    	  return true;
 39        return false;
 40    }
 41
 42    void bfs()
 43    {
 44        memset(d,-1,sizeof(d));
 45        queue<int>x;
 46        queue<int>y;
 47        queue<int>z;
 48        x.push(sx);
 49        y.push(sy);
 50        z.push(sz);
 51        d[sz][sx][sy]=0;
 52        while (!x.empty()&&!y.empty()&&!z.empty())
 53        {
 54    	  int prex = x.front();x.pop();
 55    	  int prey = y.front();y.pop();
 56    	  int prez = z.front();z.pop();
 57    	  for ( int  i = 0 ; i < 6 ; i++ )
 58    	  {
 59    		int newx = prex+dirx[i];
 60    		int newy = prey+diry[i];
 61    		int newz = prez+dirz[i];
 62    		bool flag = ok(newx,newy,newz);
 63    		if (flag)
 64    		{
 65    		    d[newz][newx][newy]=d[prez][prex][prey]+1;
 66    		    x.push(newx);
 67    		    y.push(newy);
 68    		    z.push(newz);
 69    		}
 70    	  }
 71
 72        }
 73    }
 74
 75    int main()
 76    {
 77        while (scanf("%d %d %d",&l,&r,&c))
 78        {
 79    	  if (l==0&&r==0&&c==0)
 80    		break;
 81    	  for ( int i = 0 ; i < l ; i++)
 82    	  {
 83    		for ( int j = 0 ; j < r; j++)
 84    		    scanf("%s",st[i][j]);
 85    	  }
 86    	  for ( int i = 0 ; i < l ; i++ )
 87    	  {
 88    		for ( int j = 0 ; j < r ; j ++)
 89    		{
 90    		    for ( int k =  0 ;  k < c ; k++ )
 91    		    {
 92    			  if (st[i][j][k]=='S')
 93    			  {
 94    				sx = j;
 95    				sy = k;
 96    				sz = i;
 97    			  }
 98    			  if (st[i][j][k]=='E')
 99    			  {
100    				tx = j;
101    				ty = k;
102    				tz = i;
103    			  }
104
105    		    }
106    		}
107    	  }
108    	  bfs();
109    	  int ans = d[tz][tx][ty];
110    	  if (ans==-1)
111    	  {
112    		printf("Trapped!\n");
113    	  }
114    	  else
115    	  {
116    	//	if (ans==1)
117    	//	printf("Escaped in 1 minute.\n");
118    	//	else printf("Escaped in %d minutes.\n",ans);
119    		printf("Escaped in %d minute(s).\n",ans);
120    	  }
121
122        }
123    }

相关文章

poj 3278 catch that cow

·219 字·1 分钟
http://poj.org/problem?id=3278 bfs,用到了stl的queue 代码实现 1 2 3 /* *********************************************** 4 Author :111qqz 5 Created Time :2016年02月19日 星期五 15时45分05秒 6 File Name :3278.cpp 7 ************************************************ */ 8 9 #include <algorithm> 10 #include <cstdio> 11 #include <iostream> 12 #include <cstring> 13 #include <string> 14 #include <cmath> 15 #include <map> 16 #include <stack> 17 #include <queue> 18 19 using namespace std; 20 typedef long long LL; 21 const int inf = 8E8; 22 const int N=2E5+7; 23 int d[N]; 24 int n,k; 25 void bfs() 26 { 27 queue<int> q; 28 memset(d,-1,sizeof(d)); 29 q.push(n); 30 d[n]=0; 31 while (!q.empty()) 32 { 33 int x = q.front(); 34 q.pop(); 35 if ( x==k ) 36 { 37 break; 38 } 39 int next[10]; 40 next[1]=x-1; 41 next[2]=x+1; 42 next[3]=2*x; 43 for ( int i = 1; i <= 3 ; i++ ) 44 { 45 if (next[i]>=0&&next[i]<=100000&&d[next[i]]==-1) 46 { 47 d[next[i]]=d[x]+1; 48 q.push(next[i]); 49 } 50 } 51 } 52 53 54 } 55 int main() 56 { 57 while (scanf("%d %d",&n,&k)!=EOF) 58 { 59 bfs(); 60 cout<<d[k]<<endl; 61 } 62 return 0; 63 }

POJ 1564 Sum It Up (DFS+剪枝)

·520 字·2 分钟
http://poj.org/problem?id=1564 dfs 三个参数 x,sum,k, x表示开始的坐标,sum表示当前的和,k表示这是一组答案中的第几个数,是用来记录路径的…

cf 556C Case of Matryoshkas

·377 字·1 分钟
http://codeforces.com/contest/556/problem/C 果然一晚上不睡觉会导致读错题么… 需要注意的是 如果有一个是 1 2 4 6 那么 1,2是不必拆开的….