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 ...
随机推荐
- Watering Grass (贪心,最小覆盖)
参考: https://blog.csdn.net/shuangde800/article/details/7828675 https://www.cnblogs.com/haoabcd2010/p/ ...
- python 类属性、静态方法与类方法
1. 类属性 1.1 定义 在类中方法外通过属性名 = 属性值定义的属性 访问方式: 类名.属性名 对象名.属性名 class Student: cls_id = 102 stu = Student( ...
- SpringMVC代码复制版
Lib目录 Java目录 HelloController文件代码 import org.springframework.web.servlet.ModelAndView; import org.spr ...
- 学习JDK1.8集合源码之--HashSet
1. HashSet简介 HashSet是一个不可重复的无序集合,底层由HashMap实现存储,故HashSet是非线程安全的,由于HashSet使用HashMap的Key来存储元素,而HashMap ...
- 第二周<线性回归>
可行性分析 略 sklearn.linear_model.linear_regression() 一些参数 fit_intercept 布尔型参数,表示是否计算该模型的截距 normalize 布尔型 ...
- CentOS下重启uwsgi
使用Django开发项目,每次修改内容无法刷新,重启nginx也无效,每次都重启主机, 网上搜索很多资料,发现可以重启uwsgi来解决问题,但是发现uwsgi重启每个人都不一样,很多人写了脚本重启 我 ...
- 【POJ 3261】Milk Patterns
[链接]h在这里写链接 [题意] 给你一个长度为n的序列. 问你能不能在其中找到一个最长的子串. 这个子串至少出现了k次. [题解] 长度越长,就越不可能出现k次 后缀数组+二分. N最大为2 ...
- RabbitMQ默认端口
4369 (epmd), 25672 (Erlang distribution)5672, 5671 (AMQP 0-9-1 without and with TLS)15672 (if manage ...
- win10上修改docker的镜像文件存储位置
记住:修改的是docker从服务器上拉下来的镜像文件存储位置(本地),是不是镜像源地址(服务器) 首先 win10下的docker有可视化操作界面和命令行操作,下载了docker-ce.exe双击后就 ...
- java8的stream系列教程之filter过滤集合的一些属性
贴代码 List<Student> lists = new ArrayList<>(); Student student = new Student(); student.se ...