杭电ACM online Judge上的1035题,通不过~

C++语言 码拜 9年前 (2015-05-11) 1086次浏览 0个评论

链接在这里~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;
}
10分
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;
 }
#include <stdio.h>
#include <string.h>

int row, col;
int startCol;

char instruct[15][15];
int  flag[15][15];

int check(int x, int y)
{
    if(x>=0 && x<row && y>=0 && y<col)
        return 1;
    return 0;
}

void search()
{
    char tempC;
    //int step = 0;
    flag[0][startCol-1] = 1;
    int curRow = 0;
    int curCol = startCol-1;
    int tx,ty;

    while(1)
    {
        tempC = instruct[curRow][curCol];
        if(tempC == ""W"")
        {
            tx = curRow;
            ty = curCol-1;
        } else if(tempC == ""E"") {
            tx = curRow;
            ty = curCol + 1;
        } else if(tempC == ""N"") {
            tx = curRow - 1;
            ty = curCol;
        } else if(tempC == ""S"") {
            tx = curRow + 1;
            ty = curCol;
        }
        //step++;
        if(!check(tx, ty))
        {
            printf("%d step(s) to exit\n",flag[curRow][curCol]);
            return;
        }
        if(flag[tx][ty])
        {
            break;
        }
        flag[tx][ty] = flag[curRow][curCol]+1;
        curRow = tx;
        curCol = ty;
    }
    flag[curRow][curCol]++;
    //flag[tx][ty]++;
    printf("%d step(s) before a loop of %d step(s)\n",flag[tx][ty]-1,flag[curRow][curCol]-flag[tx][ty]);
}

int main()
{
    int i;
    while(scanf("%d%d",&row, &col)!=EOF && row && col)
    {
        scanf("%d",&startCol);
        for(i=0; i<row; i++)
        {
            scanf("%s",instruct[i]);
        }
        memset(flag,0, sizeof(flag));
        search();
    }
    return 0;
}

AC代码参考下。

mxway有试一下我贴出来的代码没~实在是找不出什么漏洞啊~纠结~
30分
引用 3 楼 dongzefeng199233 的回复:

mxway有试一下我贴出来的代码没~实在是找不出什么漏洞啊~纠结~

你的代码我也没看出来有什么问题。

if (rowPos < 0 || rowPos >= row || colPos < 0 || colPos > column){
        isExit = true;
        return;//成功出去
    }

这里有问题!应该是colPos >= column~


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明杭电ACM online Judge上的1035题,通不过~
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!