ZOJ 1056 The Worm Turns
题目大意:贪吃蛇的简化版,给出一串操作命令,求蛇的最终状态是死是活。
解法:这条蛇一共20格的长度,所以用一个20个元素的队列表示,队列的每个元素是平面的坐标。每读入一条指令,判断其是否越界,是否咬到了自己。若都没有,把改点压入队列,front元素弹出。
参考代码:
#include<iostream>
#include<queue>
#include<string>
using namespace std; struct Point{
int r;
int c;
}; queue<Point> worm; bool isItself(queue<Point>,Point); int main(){
int i,n;
char str[102];
Point temp;
char x;
while(cin>>n&&n!=0){
while(!worm.empty())
worm.pop();
for(i=11;i<=30;i++){
temp.c=i;
temp.r=25;
worm.push(temp);
}
cin>>str;
i=1;
while(i<=n){
x=str[i-1];
temp=worm.back();
if(x=='W'){
temp.c-=1;
if((temp.c)<1){
cout<<"The worm ran off the board on move "<<i<<'.'<<endl;
break;
}
if(isItself(worm,temp)){
cout<<"The worm ran into itself on move "<<i<<'.'<<endl;
break;
}
worm.push(temp);
worm.pop(); }else{
if(x=='E'){
temp.c+=1;
if((temp.c)>50){
cout<<"The worm ran off the board on move "<<i<<'.'<<endl;
break;
}
if(isItself(worm,temp)){
cout<<"The worm ran into itself on move "<<i<<'.'<<endl;
break;
}
worm.push(temp);
worm.pop();
}else{
if(x=='S'){
temp.r+=1;
if((temp.r)>50){
cout<<"The worm ran off the board on move "<<i<<'.'<<endl;
break;
}
if(isItself(worm,temp)){
cout<<"The worm ran into itself on move "<<i<<'.'<<endl;
break;
}
worm.push(temp);
worm.pop();
}else{
if(x=='N'){
temp.r-=1;
if((temp.r)<1){
cout<<"The worm ran off the board on move "<<i<<'.'<<endl;
break;
}
if(isItself(worm,temp)){
cout<<"The worm ran into itself on move "<<i<<'.'<<endl;
break;
}
worm.push(temp);
worm.pop();
}
}
}
} if(i==n)
cout<<"The worm successfully made all "<<n<<" moves."<<endl;
i++;
}
} return 0;
} bool isItself(queue<Point> worm,Point p){
int k;
queue<Point> w;
w=worm;
w.pop();
for(k=1;k<19;k++){
if(w.front().r==p.r&&w.front().c==p.c)
return true;
w.pop();
}
return false;
}
ZOJ 1056 The Worm Turns的更多相关文章
- TOJ 1191. The Worm Turns
191. The Worm Turns Time Limit: 1.0 Seconds Memory Limit: 65536K Total Runs: 5465 Accepted Run ...
- The Worm Turns
The Worm Turns Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- TJU ACM-ICPC Online Judge—1191 The Worm Turns
B - The Worm Turns Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Su ...
- HDU 2782 The Worm Turns (DFS)
Winston the Worm just woke up in a fresh rectangular patch of earth. The rectangular patch is divide ...
- 【HDOJ】2782 The Worm Turns
DFS. /* 2782 */ #include <iostream> #include <queue> #include <cstdio> #include &l ...
- ZOJ 1494 Climbing Worm 数学水题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=494 题目大意: 一只蜗牛要从爬上n英寸高的地方,他速度为u每分钟,他爬完u需要 ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
随机推荐
- 二模 (2) day2
第一题: 题目描述: 在一个长方形框子里,最多有 N(0≤N≤6)个相异的点.在其中任何-个点上放一个很小的油滴,那么这个油滴会一直扩展,直到接触到其他油滴或者框子的边界.必须等一个油滴扩展完毕才能放 ...
- discuz 系列产品 在ie9下注册成功后不跳转bug处理
header.htm 把 <meta http-equiv="x-ua-compatible" content="ie=7" /> 改为 <m ...
- ARC 类型转换:显式转换 id 和 void *
http://blog.csdn.net/chinahaerbin/article/details/9471419 /* * ARC有效时三种类型转换: */ 1.__bridge ...
- scikit-learn 机器学习工具包
scikit-learn官网:http://scikit-learn.org/stable/ scikit-learn初始化学习: http://scikit-learn.org/stable/in ...
- wp8.1 C#技巧: 计时器
public MainPage() { this.InitializeComponent(); this.timer = new DispatcherTimer();//新建委托时间实例 timer. ...
- Program A-归并排序
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- Google search
filetype: active directory filetype: eg : lady gaga filetype:mp3 link: eg : link : supinfo.com(链接到su ...
- SQLSERVER数据库中批量导入数据的几种方法
第一:使用Select Into 语句 如果企业数据库都是采用SQL Server数据库的话,则可以利用select into语句实现数据的导入. select into语句的作用是把数据从另外一个数 ...
- Airbase-ng帮助
Airbase-ng 1.2 rc2 - (C) 2008-2014 Thomas d'Otreppe Original work: Martin Beck http://www.aircrack ...
- Arrays.equals()
我们知道判断字符串相等使用的是equals方法,那么要是判断两个字符串数组是否相等呢,要是char数组呢,同样的java.util.Arrays类提供了equals()方法,如下是官方API: /** ...