zoukankan      html  css  js  c++  java
  • hdu 1035 简单搜索

    1. #include <stdio.h>
    2. #include <string>
    3. #include <stdlib.h>
    4. void input(int x, int y, char map[][20]) //输入图,因为是字符串的输入,%c会出现输入空格的情况,所以用%s输入一整行
    5. {
    6. for(int i=0; i<x; i++)
    7. {
    8. scanf("%s",map[i]);
    9. }
    10. }
    11. int loop; //设立循环标志
    12. void route(int x, int y, char map[][20], int road[][20], int row, int column) //搜索图
    13. {
    14. if(road[x][y] >=2) loop=1; //出现环退出
    15. else road[x][y]++;
    16. if(map[x][y] =='N' && (x-1)>=0 && !loop) //查找,注意边界不要出去了
    17. route(x-1, y, map, road, row, column);
    18. else if(map[x][y] =='S' && (x+1) < row && !loop)
    19. route(x+1, y, map, road, row, column);
    20. else if(map[x][y] =='E' && (y+1) < column && !loop)
    21. route(x, y+1, map, road, row, column);
    22. else if(map[x][y] =='W' && (y-1) >= 0 && !loop)
    23. route(x, y-1, map, road, row, column);
    24. }
    25. int main()
    26. {
    27. int row, column, start;
    28. while( ~scanf("%d%d", &row, &column) && (row+column) !=0)
    29. {
    30. scanf("%d", &start);
    31. char map[20][20];
    32. input(row, column, map); //input
    33. int road[20][20]; //建立保存搜索路径的图
    34. memset(road, 0, sizeof(road) ); //清空,下面的loop也要注意一组数据要清空一次
    35. loop=0;
    36. route(0, start-1, map, road, row, column); //查找
    37. int count=0, count1=0;
    38. for(int i=0; i<row; i++) //遍历一遍road
    39. {
    40. for(int j=0; j<column; j++)
    41. {
    42. if(loop)
    43. {
    44. if(road[i][j] ==2) count1++; //出现环,road[x][y]会出现2
    45. else if(road[i][j] ==1) count++; //无环的路
    46. }
    47. else if(road[i][j] ==1) count++; //无环的路
    48. }
    49. }
    50. if(loop) printf("%d step(s) before a loop of %d step(s) ", count, count1);
    51. else printf("%d step(s) to exit ", count);
    52. }
    53. return 0;
    54. }
    55. /*
    56. Problem Description
    57. 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
    58. N north (up the page)
    59. S south (down the page)
    60. E east (to the right on the page)
    61. W west (to the left on the page)
    62. 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.
    63. 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.
    64. 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.
    65. Input
    66. 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.
    67. Output
    68. 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.
    69. Sample Input
    70. 3 6 5
    71. NEESWE
    72. WWWESS
    73. SNWWWW
    74. 4 5 1
    75. SESWE
    76. EESNW
    77. NWEEN
    78. EWSEN
    79. 0 0
    80. Sample Output
    81. 10 step(s) to exit
    82. 3 step(s) before a loop of 8 step(s)
    83. */





    附件列表

    • 相关阅读:
      少年中国说--正能量传播
      刚刚加入程序员的行列,希望通过博客的形式记录自己在这个领域的点点滴滴。同时分享自己的心得体会。
      java中的路径问题(getResourceAsStream/tomcat/maven/getContextpath)等各种路径问题
      java的jdbc
      maven的插件
      maven的仓库
      java9新特性
      java8新特性
      java的网络编程
      java的多线程juc
    • 原文地址:https://www.cnblogs.com/sober-reflection/p/d8e88b376184bf906e288f972427538d.html
    Copyright © 2011-2022 走看看