杭电ACM online Judge上的1035题,通不过~解决方法
杭电ACM online Judge上的1035题,通不过~
链接在这里~http://acm.hdu.edu.cn/showproblem.php?pid=1035
下面是我的代码~感觉没有错,就是通不过
~麻烦给位朋友帮我看一看啊~
------解决思路----------------------
------解决思路----------------------
你的代码我也没看出来有什么问题。
链接在这里~http://acm.hdu.edu.cn/showproblem.php?pid=1035
下面是我的代码~感觉没有错,就是通不过
#include <iostream>
using std::cin; using std::cout;
char instructionMap[10][10];
bool flag[10][10] = {0};
int countStep[10][10] = {0};
int count = -1;
int loopcount = 0;
int row, column,startPos;
bool isExit = false;
bool isLoop = false;
void dfsSearch(int rowPos, int colPos){
count++;
if (rowPos < 0 || rowPos >= row || colPos < 0 || colPos > column){
isExit = true;
return;//成功出去
}
else if (flag[rowPos][colPos]){//已经走过的点,说明进入循环
isLoop = true;
loopcount = count - countStep[rowPos][colPos];
return;
}
else{
countStep[rowPos][colPos] = count;
flag[rowPos][colPos] = 1;
switch (instructionMap[rowPos][colPos]){
case 'E':
dfsSearch(rowPos, colPos + 1);
break;
case 'S':
dfsSearch(rowPos + 1, colPos);
break;
case 'W':
dfsSearch(rowPos, colPos - 1);
break;
case 'N':
dfsSearch(rowPos - 1, colPos);
break;
}
}
}
int main()
{
while (cin >> row >> column >> startPos){
isExit = false;
isLoop = false;
count = -1;
memset(flag, 0, sizeof(flag));
loopcount = 0;
if (row == 0 || column == 0){
break;
}
for (int i = 0; i < row; i++){
for (int j = 0; j < column; j++){
cin >> instructionMap[i][j];
}
}
dfsSearch(0, startPos - 1);
if (isExit){
cout << count << " step(s) to exit";
}
else if (isLoop){
cout << count - loopcount << " step(s) before a loop of " << loopcount << " step(s)";
}
cout << std::endl;
}
return 0;
}
------解决思路----------------------
Problem : 1035 ( Robot Motion ) Judge Status : Accepted
RunId : 8641389 Language : C++ Author : huifeidmeng
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
#include<cstdio>
#include<cstring>
const int maxn=1000;
char maze[11][maxn];
int record[11][maxn];
int main()
{
int r,c,pos,i,j;
while(scanf("%d%d",&r,&c),r+c)
{
scanf("%d",&pos);
memset(maze,'\0',sizeof maze);
memset(record,0,sizeof record);
for( i=0;i<r;i++)
{
scanf("%s",maze[i]);
}
int newr=0,newc=pos-1;
bool judge=true;
while(newc>=0&&newr>=0&&maze[newr][newc]!=0)
{
record[newr][newc]++;
if(record[newr][newc]==3)
{
judge=false;
break;
}
switch(maze[newr][newc])
{
case 'N': newr--; break; //up
case 'S': newr++; break; //down
case 'E': newc++; break; //right
case 'W': newc--; break; //left
}
}
int step=0,circle=0;
for( i=0;i<r;i++)
{
for( j=0;j<c;j++)
{
if(record[i][j]==1)
step++;
else
if(record[i][j]!=0)
circle++;
}
}
if(judge)
printf("%d step(s) to exit\n",step);
else
printf("%d step(s) before a loop of %d step(s)\n",step,circle);
}
return 0;
}#include<cstdio>
#include<cstring>
const int maxn=1000;
char maze[11][maxn];
int record[11][maxn];
int main()
{
int r,c,pos,i,j;
while(scanf("%d%d",&r,&c),r+c)
{
scanf("%d",&pos);
memset(maze,'\0',sizeof maze);
memset(record,0,sizeof record);
for( i=0;i<r;i++)
{
scanf("%s",maze[i]);
}
int newr=0,newc=pos-1;
bool judge=true;
while(newc>=0&&newr>=0&&maze[newr][newc]!=0)
{
record[newr][newc]++;
if(record[newr][newc]==3)
{
judge=false;
break;
}
switch(maze[newr][newc])
{
case 'N': newr--; break; //up
case 'S': newr++; break; //down
case 'E': newc++; break; //right
case 'W': newc--; break; //left
}
}
int step=0,circle=0;
for( i=0;i<r;i++)
{
for( j=0;j<c;j++)
{
if(record[i][j]==1)
step++;
else
if(record[i][j]!=0)
circle++;
}
}
if(judge)
printf("%d step(s) to exit\n",step);
else
printf("%d step(s) before a loop of %d step(s)\n",step,circle);
}
return 0;
}
------解决思路----------------------
你的代码我也没看出来有什么问题。