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 ...
随机推荐
- WPF ScrollViewer嵌套Listbox无法滚动
最近在做项目的时候,发现listBoxzi自带的垂直滚动条有问题,经常在Add(item)的时候下面会多出一些空白的部分,而且滚动条的长度也是无规则的,一会大一会小,而且无法控制横竖滚动条的显隐藏,并 ...
- 转搞定python多线程和多进程
转自https://www.cnblogs.com/whatisfantasy/p/6440585.html 1 概念梳理: 1.1 线程 1.1.1 什么是线程 线程是操作系统能够进行运算调度的最小 ...
- day1---转自金角大王
金角大王等待唐僧的日子 Yesterday, when I was young, There were so many songs that waited to be sung. 博客园 首页 新随笔 ...
- Java连接Neo4j的两种方式
1.Neo4j数据库的两种方式 Neo4j可以以两种方式运行: Java应用程序中的嵌入式数据库 通过REST的独立服务器 不管哪一种方式,这个选择不会影响查询和使用数据库的方式. 它是由应用程序的性 ...
- 超火js库: Lodash API例子
lodash.js是一款超火的js库,在npm上平均周下载量达到了惊人的12,374,096,github start36K!大量框架都用到了lodash,包括拥有123kstart的vue 本文对比 ...
- PHP学习(语言结构语句)
switch case语句 for循环语句: foreach循环语句 常用于遍历数组,一般有两种使用方式:不取下标.取下标. (1)只取值,不取下标 <?php foreach (数组 as 值 ...
- Spring CommonsMultipartResolver上传文件小结
自从业至今,文件上传与IO流之类的调用,一直是理解比较模糊的地方,大多就这网上搜到的资料抄抄改改草草了事,内部原理一直不甚了解,今日我们通过Spring的CommonsMultipartResolve ...
- C#字符串与 byte数据的互相转换
string和byte[]的转换 (C#) string类型转成byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ( s ...
- 第三十二讲:UML类图(下)
一个类能够看到另外一个类的属性和方法,那么这两个类是关联的.
- Leetcode733.Flood Fill图像渲染
有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间. 给你一个坐标 (sr, sc) 表示图像渲染开始的像素值(行 ,列)和一个新的颜色值 newCol ...