题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2102
题目:
1 5 5 14 S*#*. .#... ..... ****. ...#. ..*.P #.*.. ***.. ...*. *.#..
YES
所以改用标记数组。。。
用bfs。。。。最开始的时候需要预处理一下.。。。。
1:如果两个对面都是传送门则会出现死循环。。。所以要把这两面设置成墙。。。
2:如果传送门的对面是墙 ,则两个设为墙。。。。
然后就是bfs了。。。
</pre><pre code_snippet_id="370769" snippet_file_name="blog_20140530_2_5736025" name="code" class="css">#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;
struct node
{
int x,y;
int step;
int a;
}start;
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
int n,m,T,ex,ey,ez;
char map[2][15][15];
bool bfs()
{
start.x=0,start.y=0, start.a=0, start.step=0;
node first,next;
queue<node>q;
map[0][0][0]='*';
q.push(start);
while(!q.empty())
{
first=q.front();
q.pop();
if(first.step>T) break;
if(first.x==ex&&first.y==ey&&first.a==ez&&first.step<=T)
return true;
for(int i=0; i<4; i++)
{
next=first;
next.x+=dx[i];
next.y+=dy[i];
++next.step;
if(next.x<0||next.x>=n||next.y<0||next.y>=m) continue;
if(map[next.a][next.x][next.y]=='#')
{
map[next.a][next.x][next.y]='*';
next.a=(next.a==1)?0:1;
q.push(next);
}
if(map[next.a][next.x][next.y]=='.')
{
map[next.a][next.x][next.y]='*'; q.push(next);
}
if(next.a==ez&&next.x==ex&&next.y==ey&&next.step<=T)
return true;
}
}
return false;
}
int main()
{
char str[25];
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&T);
for(int k=0;k<2;k++)
for(int i=0;i<n;i++)
{
scanf("%s",str);
for(int j=0;j<m;j++)
{
map[k][i][j]=str[j];
if(map[k][i][j]=='P')
ez=k,ex=i,ey=j;
}
}
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(map[0][i][j]=='#'&&map[1][i][j]=='#'){ map[0][i][j]='*';map[1][i][j]='*';}
if(map[0][i][j]=='*'&&map[1][i][j]=='#'){ map[0][i][j]='*';map[1][i][j]='*';}
if(map[0][i][j]=='#'&&map[1][i][j]=='*'){ map[0][i][j]='*';map[1][i][j]='*';}
}
if(bfs())
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
原文:http://blog.csdn.net/u014303647/article/details/27705195