您的位置:首页 > 其它

ahu-595-撒哈拉大冒险

2015-09-24 22:40 218 查看
坑点:输入不能一个个字符输入,跟hd不一样数据坑

思路 循环 x=(x%n+n)%n+搜索队列

交了我的和硕神的代码,200ms和80ms,只是因为我的是用queue库函数,硕神手撸stack,所以说亲手撸的要节省很多时间。

第一道无限循环的题学会很多。

#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>

using namespace std;

struct node{
int x,y;
};

int i,j,k,n,m,t,starx,stary,color[1005][1005],color2[1015][1015][2],bu[4][2] = {1,0,-1,0,0,1,0,-1};

char mp[1015][1015];

int main()
{
scanf("%d",&t);
while(t--)
{
bool SYM = false;
scanf("%d%d",&n,&m);
memset(color,0,sizeof(color));
for(i=0; i<n; i++)
{
scanf("%s",mp[i]);
for(j=0; j<m; j++)
{
if(mp[i][j]=='S'){
starx = i;
stary = j;
}
}
}
// bfs----------------------------------------------------------------
queue<node>point;
node node1,node2;
node1.x = starx;
node1.y = stary;
point.push(node1);
color[starx][stary] = 1;
color2[starx][stary][0] = starx;
color2[starx][stary][1] = stary;
while(!point.empty())
{
node1 = point.front();
point.pop();
if(SYM == true)break;
for(i=0; i<4; i++)
{
int X = node1.x+bu[i][0],Y = node1.y+bu[i][1];
int XX = (X%n+n)%n,YY = (Y%m+m)%m;
if(mp[XX][YY] !='#')
{
if(color[XX][YY]==0)
{
node2.x = X;
node2.y = Y;
color[XX][YY] = 1;
point.push(node2);
color2[XX][YY][0] = X;
color2[XX][YY][1] = Y;
}
else if(X!=color2[XX][YY][0] || Y!=color2[XX][YY][1])SYM = true;
}
}
}
if(SYM == true)printf("YES\n");
else printf("NO\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: