zoj 1708
简单的令人发指……
#include <iostream>
#include <queue>
using namespace std;
char MAP[10][10];
int ROOT[10][10];
int main()
{
int i,j,cnt,fa,fb;
int x,y,z;
bool loop;
char Cur;
while(cin >> x >> y >> z)
{
if(x==0&&y==0&&z==0)
break;
for(i=0;i<x;++i)
for(j=0;j<y;++j)
cin >> MAP[i][j];
for(i=0;i<10;++i)
for(j=0;j<10;++j)
ROOT[i][j] = 0;
cnt = 1;loop = false;
ROOT[0][z-1] = 1;
Cur = MAP[0][z-1];
fa = 0;fb = z-1;
while(1)
{
if(Cur=='W')
{
fb--;
}
else if(Cur=='E')
{
fb++;
}
else if(Cur=='S')
{
fa++;
}
else if(Cur=='N')
{
fa--;
}
if(fa<0||fa>=x||fb<0||fb>=y)
break;
if(ROOT[fa][fb]>0)
{
loop = true;
break;
}
cnt++;
ROOT[fa][fb] = cnt;
Cur = MAP[fa][fb];
}
if(loop==true)
cout << ROOT[fa][fb] - 1 << " step(s) before a loop of " << cnt - ROOT[fa][fb] + 1 << " step(s)" << endl;
else
cout << cnt << " step(s) to exit" << endl;
}
return 0;
}
#include <iostream>
#include <queue>
using namespace std;
char MAP[10][10];
int ROOT[10][10];
int main()
{
int i,j,cnt,fa,fb;
int x,y,z;
bool loop;
char Cur;
while(cin >> x >> y >> z)
{
if(x==0&&y==0&&z==0)
break;
for(i=0;i<x;++i)
for(j=0;j<y;++j)
cin >> MAP[i][j];
for(i=0;i<10;++i)
for(j=0;j<10;++j)
ROOT[i][j] = 0;
cnt = 1;loop = false;
ROOT[0][z-1] = 1;
Cur = MAP[0][z-1];
fa = 0;fb = z-1;
while(1)
{
if(Cur=='W')
{
fb--;
}
else if(Cur=='E')
{
fb++;
}
else if(Cur=='S')
{
fa++;
}
else if(Cur=='N')
{
fa--;
}
if(fa<0||fa>=x||fb<0||fb>=y)
break;
if(ROOT[fa][fb]>0)
{
loop = true;
break;
}
cnt++;
ROOT[fa][fb] = cnt;
Cur = MAP[fa][fb];
}
if(loop==true)
cout << ROOT[fa][fb] - 1 << " step(s) before a loop of " << cnt - ROOT[fa][fb] + 1 << " step(s)" << endl;
else
cout << cnt << " step(s) to exit" << endl;
}
return 0;
}
浙公网安备 33010602011771号