| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 564 人关注过本帖
标题:有关switch不能发挥作用的问题
只看楼主 加入收藏
弓箭手789
Rank: 1
等 级:新手上路
帖 子:11
专家分:1
注 册:2013-7-31
结帖率:100%
收藏
已结贴  问题点数:5 回复次数:2 
有关switch不能发挥作用的问题
A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are

N north (up the page)
S south (down the page)
E east (to the right on the page)
W west (to the left on the page)

For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid.

Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.

You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around.
Input
There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.
Output
For each grid in the input there is one line of output. Either the robot follows a certain number of instructions and exits the grid on any one the four sides or else the robot follows the instructions on a certain number of locations once, and then the instructions on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before it is 1.
Sample Input
3 6 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0
Sample Output
10 step(s) to exit
3 step(s) before a loop of 8 step(s)



这是acm的一道题,题目地址:http://
通过分别输出经switch处理前后x,y比对发现竟然完全一样,怀疑没有被switch处理,为了保险也将地图又除了以便发现和输入的一样说明数组中的字符没有错误,但switch就是不处理x,y,百思不得其解求高手赐教
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
       char fangwei;
       int step;
}map[15][15];
int main()
{
    int i,j,x=0,y=0,row,col,strat;
    while (scanf ("%d %d %d",&row,&col,&strat)!=EOF&&row+col+strat)
    {
          getchar();
          for (i=1;i<=row;i++,getchar())
              for (j=1;j<=col;j++)
                  scanf ("%c",&map[i][j].fangwei);
          j=strat;
          i=1;
          memset(map,0,sizeof(map));
          map[i][j].step=1;
          while (1)
          {
                x=i;
                y=j;
                printf("%d %d\n",x,y);
                switch (map[i][j].fangwei)
                {
                case 'E':y=j+1;break;
                case 'W':y=j-1;break;
                case 'N':x=i-1;break;
                case 'S':x=i+1;break;
                }
                printf("%d %d\n",x,y);
                printf("%d\n",map[x][y].step);printf("paude\n");//
                if (map[x][y].step)
                {
                   printf("%d step(s) before a loop of %d step(s)\n",map[x][y].step-1,map[i][j].step-map[x][y].step+1);   
                   break;
                }
                if (x<1||y<1||x>row||y>col)
                {
                   printf("%d step(s) to exit\n",map[i][j].step);
                   break;
                }
                else
                {
                     map[x][y].step=map[i][j].step+1;
                     i=x;
                     j=y;
                }               
          }
    }
    return 0;
}
搜索更多相关主题的帖子: direction possible example suppose through 
2013-08-10 17:56
Mhugh
Rank: 2
来 自:广西
等 级:论坛游民
帖 子:7
专家分:33
注 册:2013-8-11
收藏
得分:4 
switch (map[i][j].fangwei)
                {
                case 'E':y=j+1;break;
                case 'W':y=j-1;break;
                case 'N':x=i-1;break;
                case 'S':x=i+1;break;
                }
是进不到这里吗?
你把最后的case改成default看看吧

我不在乎对你焦头烂额,我只在乎我能够把你一次次的写对。
2013-08-11 09:57
弓箭手789
Rank: 1
等 级:新手上路
帖 子:11
专家分:1
注 册:2013-7-31
收藏
得分:0 
我的错误在于读入大量字符之后用memset清零了,memset应该加在循环读入之前。
2013-08-12 11:05
快速回复:有关switch不能发挥作用的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020819 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved