首页 > 代码库 > hdu1035

hdu1035

hdu1035是个模拟题,依照图上的方向走,走出grid,算出步数。走入循环,则输入循环体的个数有多少个,和走入循环体之前的个数多少个

#include<stdio.h>

#include<iostream>
using namespace std;


struct node
{
char c;
int count;
};
node nd[1005][1005];
int n,m,root;
int step;
int loop;
int i,j;
int ii,jj;
bool f(int pi,int pj)
{
if(pi<1||pi>n||pj<1||pj>m) return false;
return true;
}
int main()
{
while(cin>>n>>m&&n&&m)
{
cin>>root;
step=0;
loop=0;
if(root<0||root>m)break;
for(i=0;i<1005;i++)
{
for(j=0;j<1005;j++)
{
nd[i][j].c=0;
nd[i][j].count=0;
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cin>>nd[i][j].c;
}
}

i=1;
j=root;
nd[i][j].count=1;
step=1;
while(1)
{
//cout<<"i="<<i<<",j="<<j<<",nd[i][j]="<<nd[i][j].count<<",nd[i][j].c="<<nd[i][j].c<<endl;
ii=i;
jj=j;
switch(nd[i][j].c)
{
case ‘N‘:
ii--;
break;
case ‘S‘:
ii++;
break;
case ‘W‘:
jj--;
break;
case ‘E‘:
jj++;
break;
}
if(!f(ii,jj))
{
cout<<step<<" step(s) to exit"<<endl;
   break;
}
if(nd[ii][jj].count==0)
{
nd[ii][jj].count=nd[i][j].count+1;
step=nd[ii][jj].count;
i=ii;
j=jj;
}
else
{
loop=nd[i][j].count-nd[ii][jj].count+1;
step=nd[ii][jj].count-1;
cout<<step<<" step(s) before a loop of "<<loop<<" step(s)"<<endl;
break;
}

}


}
return 0;
}

hdu1035