hdu 1035 Robot Motion
来源:互联网 发布:php redis 扩展 编辑:程序博客网 时间:2024/05/16 18:01
原题:
http://acm.hdu.edu.cn/showproblem.php?pid=1035
思路:
1.建立二维数组grid[12][12]用来存储地图。
2.step[12][12]并初始化为0,其中step[i][j]表示到grid[i][j]需要的步数。
3.用变量ni,nj记录下一步要移动到的位置,对ni,nj做判断。
4.如果出了field,则结束,输出step[i][j]。
5.如果step[ni][nj]非零,表示进入循环,则循环前共有a=step[ni][nj]-1步,循环有b=step[i][j]-a步。
代码:
#include <iostream>using namespace std;#define going trueint main(){ int row,col,start; while(cin>>row>>col>>start && row+col+start>0) { char grid[12][12];//周围多加一圈,防止越界 int step[12][12]={0}; int i,j,ni,nj; for(i=1;i<=row;i++) for(j=1;j<=col;j++) cin>>grid[i][j]; //进入 i=1; j=start; step[i][j]=1; //开始 while(going) { //读取下一步 switch(grid[i][j]) { case 'N': ni=i-1; nj=j; break; case 'S': ni=i+1; nj=j; break; case 'W': ni=i; nj=j-1; break; case 'E': ni=i; nj=j+1; break; } //判断走出 if(ni*nj==0 || ni==row+1 || nj==col+1) { cout<<step[i][j]<<" step(s) to exit"<<endl; break; } else { if(step[ni][nj]!=0)//即将进入loop { int a=step[ni][nj]-1;//steps before loop int b=step[i][j]-a;//steps in loop cout<<a<<" step(s) before a loop of "<<b<<" step(s)"<<endl; break; } else { step[ni][nj] = step[i][j]+1; i = ni; j = nj; } } } } return 0;}
0 0
- hdu 1035 Robot Motion
- HDU 1035 Robot Motion
- hdu 1035 Robot Motion
- hdu 1035 Robot Motion
- hdu 1035 Robot Motion
- HDU 1035 Robot Motion
- hdu 1035 Robot Motion
- hdu 1035 robot motion
- HDU-1035Robot Motion
- hdu 1035 Robot Motion
- Robot Motion HDU 1035
- (hdu)1035 Robot Motion
- hdu 1035 Robot Motion
- HDU 1035 - Robot Motion
- HDU 1035 Robot Motion
- HDU 1035 Robot Motion
- hdu 1035 Robot Motion
- HDU 1035 Robot Motion
- 【BZOJ1093】[ZJOI2007]最大半连通子图【SCC】【DAG】【DP】
- 设计模式笔记:策略模式
- 常见的网络设备
- 2016-04-29知识整理
- android里面的USB功能----Host模式
- hdu 1035 Robot Motion
- Quartz2D图片裁剪圆的做法
- 获取List集合中的重复数据和重复次数(适用于有着连续的相同数据项时)
- Leetcode 149. Max Points on a Line
- hdu3294 manacher算法
- Android蓝牙4.0之GATT
- J2EE常见路径处理
- Python 模块学习 logging
- setTextColor设置