您的位置:首页 > 其它

poj 1573 简单模拟

2013-01-04 19:17 351 查看
做几个简单模拟题

#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <queue>
using namespace std;

///宏定义
const int INF = 20000000;
const int MAXN = 15;
///全局变量 和 函数
char maze[MAXN][MAXN];
bool vis[MAXN][MAXN];
int rows, cols, entry;
int main()
{
///变量定义
int i, j, k;
///操作执行
while(1)
{
scanf("%d %d %d", &rows, &cols, &entry);
getchar();
if(rows == 0 && cols == 0 && entry == 0)
break;
for(i = 1; i <= rows; ++i)
{
gets(maze[i] + 1);
}
memset(vis, false, sizeof(vis));
bool flag = false;
int posy, posx;
int steps = 0;
posy = 1;
posx = entry;
vis[posy][posx] = true;
while(1)
{
steps++;
if(maze[posy][posx] == 'N')
{
posy -= 1;
}
else if(maze[posy][posx] == 'W')
{
posx -= 1;
}
else if(maze[posy][posx] == 'S')
{
posy += 1;
}
else
{
posx += 1;
}
if(posy <= 0 || posy > rows || posx <= 0 || posx > cols)
break;
if(vis[posy][posx])
{
flag = true;
break;
}
else
vis[posy][posx] = true;
}
if(flag)
{
memset(vis, false, sizeof(vis));
vis[posy][posx] = true;
int cnt = 0;
while(1)
{
cnt++;
if(maze[posy][posx] == 'N')
{
posy -= 1;
}
else if(maze[posy][posx] == 'W')
{
posx -= 1;

}
else if(maze[posy][posx] == 'S')
{
posy += 1;

}
else
{
posx += 1;
}

if(vis[posy][posx])
{
break;
}
else
vis[posy][posx] = true;
}
printf("%d step(s) before a loop of %d step(s)\n", steps - cnt, cnt);
}
else
printf("%d step(s) to exit\n", steps);
}

///结束
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: