poj 1573 Robot Motion
2015-08-22 17:14
260 查看
action_map纪录经过每个点的次数
但其实只可能是0,1只要再次经过标记为1的点时就一定是走到圈里了
刚开始写的++,不是直接附值为1,就超时了……竟然超时了!!!
然后改了又wa……嗯……是没有初始化
哇靠其实应该不用也没事啊???
只能说c++是一个神奇的语言……
但其实只可能是0,1只要再次经过标记为1的点时就一定是走到圈里了
刚开始写的++,不是直接附值为1,就超时了……竟然超时了!!!
然后改了又wa……嗯……是没有初始化
哇靠其实应该不用也没事啊???
只能说c++是一个神奇的语言……
#include <iostream> #include <cstdio> using namespace std; char map[12][12]; int action_map[12][12]; int row,width,spot; int step; //the number of the steps int loop; //counting the steps of the loop int action_in_loop(int i,int j) { action_map[i][j]=2; while (i>=1&&i<=row&&j>=1&&j<=width) { if (map[i][j]=='N') { if (2==action_map[i-1][j]) { return 0; } action_map[i-1][j]++; loop++; i--; } if (map[i][j]=='S') { if (2==action_map[i+1][j]) { return 0; } action_map[i+1][j]++; loop++; i++; } if (map[i][j]=='E') { if (2==action_map[i][j+1]) { return 0; } action_map[i][j+1]++; loop++; j++; } if (map[i][j]=='W') { if (2==action_map[i][j-1]) { return 0; } action_map[i][j-1]++; loop++; j--; } } return 1; } int action_in_map(int start) //the situation that the robot move in the map { action_map[1][start]=1; int i=1,j=start; while (i>=1&&i<=row&&j>=1&&j<=width) { if (map[i][j]=='N') { if (1==action_map[i-1][j]) { action_in_loop(i, j); return 0; } action_map[i-1][j]=1; step++; i--; } if (map[i][j]=='S') { if (1==action_map[i+1][j]) { action_in_loop(i, j); return 0; } action_map[i+1][j]=1; step++; i++; } if (map[i][j]=='E') { if (1==action_map[i][j+1]) { action_in_loop(i, j); return 0; } action_map[i][j+1]=1; step++; j++; } if (map[i][j]=='W') { if (1==action_map[i][j-1]) { action_in_loop(i, j); return 0; } action_map[i][j-1]=1; step++; j--; } } return 1; } int main(int argc, const char * argv[]) { int spot; char m; while (cin>>row>>width>>spot) { if (row==0&&width==0&&spot==0) { return 0; } memset(map,0,sizeof(map)); memset(action_map,0,sizeof(map)); int i,j; step=0,loop=0; getchar(); for (i=1; i<=row; i++) { for (j=1; j<=width; j++) { m=getchar(); map[i][j]=m; action_map[i][j]=0; } getchar(); } /*for (i=1; i<=row; i++) { for (j=1; j<=width; j++) { cout<<action_map[i][j]<<" "; } cout<<endl; }*/ if(action_in_map(spot)==1) cout<<step<<" step(s) to exit"<<endl; else cout<<step-loop<<" step(s) before a loop of "<<loop+1<<" step(s)"<<endl; } return 0; }
相关文章推荐
- 我的Linux系统的VIMRC
- Azure中的WEB项目与Azure的MySQL连接
- 我的第一篇博客,就要开始啦
- 强制横屏
- XHTML的使用规范
- 湖南省第八届大学生计算机程序设计竞赛1112: 机器人的指令
- JMS - 消息选择器
- Linux下进程之间通信机制
- POJ 3468 A Simple Problem with Integers(线段树成段更新+lazy标记)
- Android学习笔记(四十):Preference的使用
- POJ 3692:Kindergarten(最大的使命)
- Myeclipse 突然打不开的问题
- 05.第一个oc的类
- 关于Cocopod
- python写csv文件
- throw和throws用法
- hdu 3047 Zjnu Stadium(加权并查集)2009 Multi-University Training Contest 14
- Python第一天 - list\字符串截取
- UVA 11020 Efficient Solutions (BST,Splay树)
- 地址的偏移量