Erratic Ants |
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB |
Total submit users: 11, Accepted users: 5 |
Problem 13076 : No special judgement |
Problem description |
The ants of a particular colony are in search of food. Unfortunately hidden dangers are all around the colony which makes foraging difficult. There are traps, obstacles, and predators lurking about. Fortunately, the colony has the perfect ant for the job. Max is neither a smart ant nor an efficient ant but he has got blind luck on his side. In all of his wanderings, he has always managed to stay on safe ground and he (eventually) always finds a source of food to report back to the colony. |
Input |
The first integer in the input, 1<=n<=100, denotes the number of paths to food that Max has reported back. This is followed by a blank line and then descriptions of each of the n paths. Each path description begins with an integer, s (0<=s<=60), which denotes the number of steps taken in the path. The next s lines contain directional steps expressed as upper-case characters N, E, S, and W corresponding to steps taken in the directions north, east, south, and west respectively. Each step moves Max one unit of distance. Max’s paths always start at the colony and end at a food source. Between each pair of path descriptions is a blank line. |
Output |
For each given path, give the number of steps found to be in an optimal (shortest) path. |
Sample Input |
3 8 S E E E N W S S 4 S E N W 3 S E N |
Sample Output |
4 0 3 |
一开始读了感觉还是蛮简单的 主要是没给出图 所以需要我们自己去建图
我们可以模拟出一个二维的图 这个点的值为1 说明这个点是可以走到的
但是单纯的简单构图 又会出现问题 当遇到第三个样例的时候就会WA
所以我们还要想办法去表示 这个点能不能到达相邻的点
所以我就构造一个结构体 表示一个点能否向东南西北走。。
但是我一开始的时候又出现了一个问题 就是main函数中 对点状态进行赋值的时候会出现覆盖的情况
如果该点不是第一次出现 第二次 第三次。。出现的状态就会把之前的状态进行覆盖 所以之前还要判断该点之前有木有出现过。。
然后大胆的BFS就可以啦。。。
图论构图不易 且行且真心
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <queue>
#include <map>
#define eps 1e-8
#define op operator
#define MOD 10009
#define MAXN 100100
#define INF 0x7fffffff
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FOV(i,a,b) for(int i=a;i>=b;i--)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define REV(i,a,b) for(int i=a-1;i>=b;i--)
#define MEM(a,x) memset(a,x,sizeof a)
#define ll __int64
using namespace std;
int mp[140][140];
int vis[140][140];
int xx,yy;
struct node
{
int x,y;
int step;
bool operator<(const node p)const
{
return step>p.step;
}
};
struct point
{
// int x,y;
int n,s,w,e;
};
point po[140][140];
node p,q;
priority_queue<node> Q;
int dir[4][2]={{0,-1},{1,0},{0,1},{-1,0}};
void bfs()
{
// cout<<"aaaaaa"<<endl;
p.x=70; p.y=70;
p.step=0;
Q.push(p);
vis[70][70]=1;
while(!Q.empty())
{
// cout<<"nnnnn"<<endl;
q=Q.top();
Q.pop();
if(q.x==xx&&q.y==yy)
{
printf("%d\n",q.step);
return;
}
for(int i=0;i<4;i++)
{
p.x=q.x+dir[i][0]; p.y=q.y+dir[i][1];
if(p.x>=0&&p.x<140&&p.y>=0&&p.y<140&&!vis[p.x][p.y]&&mp[p.x][p.y])
{
if(i==0&&po[q.x][q.y].n)
{
vis[p.x][p.y]=1;
p.step=q.step+1;
Q.push(p);
}
if(i==1&&po[q.x][q.y].e)
{
vis[p.x][p.y]=1;
p.step=q.step+1;
Q.push(p);
}
if(i==2&&po[q.x][q.y].s)
{
vis[p.x][p.y]=1;
p.step=q.step+1;
Q.push(p);
}
if(i==3&&po[q.x][q.y].w)
{
vis[p.x][p.y]=1;
p.step=q.step+1;
Q.push(p);
}
}
}
}
}
int main()
{
//freopen("sample.txt","r",stdin);
int tc;
scanf("%d",&tc);
getchar();
char tmp[50];
while(tc--)
{
// gets(tmp);
int n;
scanf("%d",&n);
// getchar();
MEM(mp,0);
int x=70; int y=70;
mp[x][y]=1;
po[70][70].n=0;
po[70][70].s=0;
po[70][70].w=0;
po[70][70].e=0;
// MEM(edge,0);
for(int i=0;i<n;i++)
{
int x1=x,y1=y;
char ch[100];
scanf("%s",ch);
getchar();
if(ch[0]=='S')
{
y++;
if(mp[x][y])
{
po[x][y].n=1;
}
else
{
po[x][y].n=1;
po[x][y].s=0;
po[x][y].w=0;
po[x][y].e=0;
mp[x][y]=1;
}
if(mp[x1][y1])
{
po[x1][y1].s=1;
}
else
{
po[x1][y1].n=0;
po[x1][y1].s=1;
po[x1][y1].w=0;
po[x1][y1].e=0;
}
}
if(ch[0]=='N')
{
y--;
if(mp[x][y])
{
po[x][y].s=1;
}
else
{
po[x][y].n=0;
po[x][y].s=1;
po[x][y].w=0;
po[x][y].e=0;
mp[x][y]=1;
}
if(mp[x1][y1])
{
po[x1][y1].n=1;
}
else
{
po[x1][y1].n=1;
po[x1][y1].s=0;
po[x1][y1].w=0;
po[x1][y1].e=0;
}
}
if(ch[0]=='W')
{
x--;
if(mp[x][y])
{
po[x][y].e=1;
}
else
{
po[x][y].n=0;
po[x][y].s=0;
po[x][y].w=0;
po[x][y].e=1;
mp[x][y]=1;
}
if(mp[x1][y1])
{
po[x1][y1].w=1;
}
else
{
po[x1][y1].n=0;
po[x1][y1].s=0;
po[x1][y1].w=1;
po[x1][y1].e=0;
}
}
if(ch[0]=='E')
{
x++;
if(mp[x][y])
{
po[x][y].w=1;
}
else
{
po[x][y].n=0;
po[x][y].s=0;
po[x][y].w=1;
po[x][y].e=0;
mp[x][y]=1;
}
if(mp[x1][y1])
{
po[x1][y1].e=1;
}
else
{
po[x1][y1].n=0;
po[x1][y1].s=0;
po[x1][y1].w=0;
po[x1][y1].e=1;
}
}
}
xx=x; yy=y;
// cout<<x<<" "<<y<<endl;
MEM(vis,0);
bfs();
while(!Q.empty())
Q.pop();
// cout <<'t' <<endl;
}
return 0;
}