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 ...
随机推荐
- libpng交叉编译安装
tar xzf libpng-1.5.22.tar.gz cd libpng-1.5.22 mkdir tmp 打开Makefile文件并修改CC=arm-linux-gcc ./configure ...
- python 字典排序 关于sort()、reversed()、sorted()
一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...
- CodeForces 546B-Soldier and Badges
题意: 给出一些数字,要求每个数字都不一样需要增加几 思路: 先排序,然后一个个增加,最后求总和差 代码如下: #include <iostream> #include <cstdi ...
- 详解C++中指针(*)、取地址(&)、解引用(*)与引用(&)的区别 (完整代码)
一.初步了解--指针与取地址 先看程序: #include<cstdio> int main(void) { int num = 7; int *p = # printf( ...
- git 克隆项目 与 分支简单操作
git clone http://abcde.com/myproject/abc.git 克隆远程项目到本地githome文件夹git branch -a 查看所有分支 包括远程和本地 *号开头表示当 ...
- 计算系数(noip2011)
[问题描述]给定一个多项式(ax + by)^k,请求出多项式展开后(x^n)*(y^m)项的系数.[输入]输入文件名为 factor.in.共一行,包含 5 个整数,分别为a,b,k,n,m,每两个 ...
- <二叉树的基本操作(有层次遍历)>
#include<stdio.h> #include<stdlib.h> #include<string.h> #define num 100 #define OK ...
- 关于jquery.bind
随着现在JQuery这个javascript的越来越强大,在我们平常的前端UI开发,如果不使用JQuery,说明你已经很out了.今天我们来学习一下 JQuery的bind事件.虽然,这个话题被很 ...
- SVG 2D入门9 - 蒙板
SVG支持的蒙板 SVG支持多种蒙板特效,使用这些特性,我们可以做出很多很炫的效果.至于中文中把mask叫做"蒙板"还是"遮罩"就不去区分了,这里都叫做蒙板吧. ...
- Mifare Classic Tool(MCT)汉化版
2.0.4 到 2.0.6的更改: 注意:本汉化版本可能不稳定,与此发生的一切后果与作者和汉化者无关. Version : * Bugfix: Fixed crash which occurred i ...