您的位置:首页 > 其它

poj 1573 Robot Motion

2015-08-22 17:14 260 查看
action_map纪录经过每个点的次数

但其实只可能是0,1只要再次经过标记为1的点时就一定是走到圈里了

刚开始写的++,不是直接附值为1,就超时了……竟然超时了!!!

然后改了又wa……嗯……是没有初始化

哇靠其实应该不用也没事啊???

只能说c++是一个神奇的语言……

#include <iostream>
#include <cstdio>
using namespace std;

char map[12][12];
int action_map[12][12];
int row,width,spot;
int step;   //the number of the steps
int loop;   //counting the steps of the loop

int action_in_loop(int i,int j)
{
action_map[i][j]=2;
while (i>=1&&i<=row&&j>=1&&j<=width) {
if (map[i][j]=='N') {
if (2==action_map[i-1][j]) {
return 0;
}
action_map[i-1][j]++;
loop++;
i--;
}
if (map[i][j]=='S') {
if (2==action_map[i+1][j]) {
return 0;
}
action_map[i+1][j]++;
loop++;
i++;
}
if (map[i][j]=='E') {
if (2==action_map[i][j+1]) {
return 0;
}
action_map[i][j+1]++;
loop++;
j++;
}
if (map[i][j]=='W') {
if (2==action_map[i][j-1]) {
return 0;
}
action_map[i][j-1]++;
loop++;
j--;
}
}
return 1;
}

int action_in_map(int start)    //the situation that the robot move in the map
{
action_map[1][start]=1;
int i=1,j=start;
while (i>=1&&i<=row&&j>=1&&j<=width) {
if (map[i][j]=='N') {
if (1==action_map[i-1][j]) {
action_in_loop(i, j);
return 0;
}
action_map[i-1][j]=1;
step++;
i--;
}
if (map[i][j]=='S') {
if (1==action_map[i+1][j]) {
action_in_loop(i, j);
return 0;
}
action_map[i+1][j]=1;
step++;
i++;
}
if (map[i][j]=='E') {
if (1==action_map[i][j+1]) {
action_in_loop(i, j);
return 0;
}
action_map[i][j+1]=1;
step++;
j++;
}
if (map[i][j]=='W') {
if (1==action_map[i][j-1]) {
action_in_loop(i, j);
return 0;
}
action_map[i][j-1]=1;
step++;
j--;
}
}
return 1;
}

int main(int argc, const char * argv[]) {
int spot;
char m;
while (cin>>row>>width>>spot) {
if (row==0&&width==0&&spot==0) {
return 0;
}
memset(map,0,sizeof(map));
memset(action_map,0,sizeof(map));
int i,j;
step=0,loop=0;
getchar();
for (i=1; i<=row; i++)
{
for (j=1; j<=width; j++)
{
m=getchar();
map[i][j]=m;
action_map[i][j]=0;
}
getchar();
}
/*for (i=1; i<=row; i++)
{
for (j=1; j<=width; j++)
{
cout<<action_map[i][j]<<" ";
}
cout<<endl;
}*/
if(action_in_map(spot)==1)
cout<<step<<" step(s) to exit"<<endl;
else
cout<<step-loop<<" step(s) before a loop of "<<loop+1<<" step(s)"<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: