据说是道BFS,但是好像只有遇到解直接输出跟BFS有点关系,除此之外完全纯模拟
提交时间 2016-11-09 17:12
耗时/内存 52ms , 16410kb (巨慢)
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
const int maxn = 20;
using namespace std;
int cnt;
int ten1 = 1,ten2 = 1;
int dx[5] = {0,1,0,-1,0};
int dy[5] = {0,0,1,0,-1};
char map[maxn][maxn];
int fx,fy,cx,cy;
struct node
{
int x,y;
};
bool check(node ha)
{
if(ha.x <= 0||ha.x > 10||ha.y <= 0||ha.y > 10||map[ha.x][ha.y] == '*')
{
//cout<<"check is 0"<<"\n"<<endl;
return false;
}
//cout<<"check is 1"<<"\n";
return true;
}
inline void bfs()
{
node fj,tt;
fj.x = fx;
fj.y = fy;
tt.x = cx;
tt.y = cy;
for(;;)
{
cnt++;
//cout<<"这里是cnt: "<<cnt<<endl;
if(cnt >= 1000) {printf("0\n");return ;}
if(ten1 == 1) { fj.x -= 1;if(check(fj) == false) { ten1 = 2; fj.x += 1;} }
else if(ten1 == 2) { fj.y += 1; if(check(fj) == false) { ten1 = 3; fj.y -= 1;} }
else if(ten1 == 3) { fj.x += 1; if(check(fj) == false) { ten1 = 4; fj.x -= 1;} }
else if(ten1 == 4) { fj.y -= 1; if(check(fj) == false) { ten1 = 1; fj.y += 1;} }
//cout<<"哔哔这里是农民约翰("<<fj.x<<","<<fj.y<<")收到请回答"<<endl;
if(ten2 == 1) { tt.x -= 1;if(map[tt.x][tt.y] == '*' || check(tt) == false) {ten2 = 2;tt.x += 1;} }
else if(ten2 == 2) { tt.y += 1; if(check(tt) == false) { ten2 = 3; tt.y -= 1;} }
else if(ten2 == 3) { tt.x += 1; if(check(tt) == false) { ten2 = 4; tt.x -= 1;} }
else if(ten2 == 4) { tt.y -= 1; if(check(tt) == false) { ten2 = 1; tt.y += 1;} }
//cout<<"哔哔这里是两只塔姆沃思牛("<<tt.x<<","<<tt.y<<")我们已经收到,再见垃圾约翰~"<<endl;
//cout<<"约翰:“WTF???”"<<endl;
//以上为模拟行进
if(fj.x == tt.x&&fj.y == tt.y) {printf("%d\n",cnt);return ;}
}
}
int main()
{
for(int i = 1;i <= 10;i++)
for(int j = 1;j <= 10;j++)
{
cin>>map[i][j];
if(map[i][j] == 'F') {fx = i;fy = j;}
if(map[i][j] == 'C') {cx = i;cy = j;}
}
bfs();
return 0;
}
THE END
By Peacefuldoge