poj1573
题意:给出一个矩形,N,E,S,W分别代表进行移动的方向,如果走出矩形网格则输出经过的网格数,如果在矩形网格内循环,则输出没进入循环之前所走过的网格数和循环所经过的网格数;
思路:创建两个数组,一个字符数组存每个网格中所要进行的操作,另一个整型数组代表状态,0代表没走过,1代表走过了,然后模拟;
再提醒一点,建立的字符和整型数组要和题目中建里的一模一样,因为这个我又WA了一发、
#include<iostream>
#include<cstring>
using namespace std;
const int qq=15;
int gid[qq][qq];char s[qq][qq];
int main()
{
int n,m,k;
while(cin >> n >> m >> k)
{
cin.get();
if(n==0&&m==0&&k==0) break;
memset(gid,0,sizeof(gid));
for(int i=0;i<=m+1;++i){
gid[0][i]=1;gid[n+1][i]=1; //外围标记
}
for(int i=0;i<=n+1;++i){ //外围标记
gid[i][0]=1;gid[i][m+1]=1;
}
for(int j,i=1;i<=n;++i){
for(j=1;j<=m;++j)
s[i][j]=cin.get();
cin.get();
}
int x,y;x=k;y=1;int tot=0;
while(!gid[y][x]){
while(!gid[y][x]&&s[y][x]=='N'){
gid[y][x]=1;y-=1;++tot;
}
while(!gid[y][x]&&s[y][x]=='E'){
gid[y][x]=1;x+=1;++tot;
}
while(!gid[y][x]&&s[y][x]=='S'){
gid[y][x]=1;y+=1;++tot;
}
while(!gid[y][x]&&s[y][x]=='W'){
gid[y][x]=1;x-=1;++tot;
}
}
if(x<1||y<1||x>m||y>n) cout << tot << " step(s) to exit\n";
else{
int count=0;
while(gid[y][x]){ //计算循环所经过的网格数 此时1,0的意义互换
while(gid[y][x]&&s[y][x]=='N'){
gid[y][x]=0;y-=1;++count;
}
while(gid[y][x]&&s[y][x]=='E'){
gid[y][x]=0;x+=1;++count;
}
while(gid[y][x]&&s[y][x]=='S'){
gid[y][x]=0;y+=1;++count;
}
while(gid[y][x]&&s[y][x]=='W'){
gid[y][x]=0;x-=1;++count;
}
}
cout << tot-count << " step(s) before a loop of " << count << " step(s)\n";
}
}
}
poj1573的更多相关文章
- POJ-1573 Robot Motion模拟
题目链接: https://vjudge.net/problem/POJ-1573 题目大意: 有一个N*M的区域,机器人从第一行的第几列进入,该区域全部由'N' , 'S' , 'W' , 'E' ...
- poj1573 Robot Motion(DFS)
题目链接 http://poj.org/problem?id=1573 题意 一个机器人在给定的迷宫中行走,在迷宫中的特定位置只能按照特定的方向行走,有两种情况:①机器人按照方向序列走出迷宫,这时输出 ...
- POJ1573(Robot Motion)--简单模拟+简单dfs
题目在这里 题意 : 问你按照图中所给的提示走,多少步能走出来??? 其实只要根据这个提示走下去就行了.模拟每一步就OK,因为下一步的操作和上一步一样,所以简单dfs.如果出现loop状态,只要记忆每 ...
- poj1573 Robot Motion
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12507 Accepted: 6070 Des ...
- poj1573 模拟
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11270 Accepted: 5487 Des ...
- POJ1573——Robot Motion
Robot Motion Description A robot has been programmed to follow the instructions in its path. Instruc ...
- POJ1573 Robot Motion(模拟)
题目链接. 分析: 很简单的一道题, #include <iostream> #include <cstring> #include <cstdio> #inclu ...
- poj1573&&hdu1035 Robot Motion(模拟)
转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接: HDU:pid=1035">http://acm.hd ...
- poj1573模拟
Robot Motion Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u Java ...
- 快速切题 poj1573
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10708 Accepted: 5192 Des ...
随机推荐
- 如何解决Firefox浏览器地址栏中文搜索速度很慢
一.插件安装 之前使用Chrome浏览器,习惯在地址栏中直接进行中文搜索.转到Firefox之后,突然发现在地址栏进行中文搜索,访问速度会很慢. 可以使用插件解决这个问题:Omnibar 插件地址:h ...
- php 单向散列加密
1.加密文件 <?php //sha1_en.php header("content-type:text/html;charset=utf-8"); $str = " ...
- ie6中兼容性问题总结
针对firefox ie6 ie7 ie8的css样式中的line-height属性,以前我们大部分都是用!important来hack,对于ie6和firefox测试可以正常显示,但是ie7以上对! ...
- JAVA-WEB-错误之-'OPTION SQL_SELECT_LIMIT=DEFAULT'
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version ...
- closest和parents方法区别
今天第一次看到closest方法,以前也从来没用过. 该方法从元素本身开始往上查找,返回最近的匹配的祖先元素. 1.closest查找开始于自身,parents开始于元素父级 2.closest向上查 ...
- du,df区别
1.记住命令 du:disk Usage -h, --human-readable print sizes in human readable format df:disk free 2.区别 du ...
- init()方法必须使用super.init(config)的原因--Servlet
原 因: 一个servlet在它的init()方法中传递它的ServletConfig实例,在其他的方法中却不可以.当一个servlet在 init()方法外需要调用config对象时就会产生问题.使 ...
- iOS开发 分享到QQ空间提示"分享失败 应用不存在"
本人遇到该问题的原因是配置SDK初始化时的APPID错误,可以参考下shareSDK的集成文档中的一段话: 可选:支持QQ所需的相关配置及代码 登录QQ互联(http://connect.qq.com ...
- Java练习 SDUT-2746_大小写转换
大小写转换 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description X现在要学习英文以及各种稀奇古怪的字符的了.现在他想把一串字 ...
- Java练习 SDUT-2504_多项式求和
多项式求和 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 多项式描述如下: 1 - 1/2 + 1/3 - 1/4 + ...