zoj1056
来源:互联网 发布:中国人的智慧知乎 编辑:程序博客网 时间:2024/05/27 20:14
题目大意:
模拟一个简单的贪吃蛇游戏,有东(E)南(S)西(W)北(N)四个方向,当走到自己内部或者一个障碍物时结束
解题思路:
模拟移动过程
代码如下:
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;typedef struct{ int row,col;}ro;ro road[25];char worm[50];int head,tail;int move_and_judge(char c){ int headrow,headcol,i; if(c=='E') { headcol=road[1].col+1;headrow=road[1].row; if(headcol>50||headcol<1||headrow>50||headrow<1) { return -1; } for(i=20;i>1;i--) { road[i].row=road[i-1].row; road[i].col=road[i-1].col; if(headrow==road[i].row&&headcol==road[i].col) { return 1; } } road[1].row=headrow;road[1].col=headcol; } else if(c=='W') { headrow=road[1].row;headcol=road[1].col-1; if(headcol>50||headcol<1||headrow>50||headrow<1) { return -1; } for(i=20;i>1;i--) { road[i].row=road[i-1].row; road[i].col=road[i-1].col; if(headrow==road[i].row&&headcol==road[i].col) { return 1; } } road[1].row=headrow;road[1].col=headcol; } else if(c=='S') { headrow=road[1].row+1;headcol=road[1].col; if(headcol>50||headcol<1||headrow>50||headrow<1) { return -1; } for(i=20;i>1;i--) { road[i].row=road[i-1].row; road[i].col=road[i-1].col; if(headrow==road[i].row&&headcol==road[i].col) { return 1; } } road[1].row=headrow;road[1].col=headcol; } else if(c=='N') { headrow=road[1].row-1;headcol=road[1].col; if(headcol>50||headcol<1||headrow>50||headrow<1) { return -1; } for(i=20;i>1;i--) { road[i].row=road[i-1].row; road[i].col=road[i-1].col; if(headrow==road[i].row&&headcol==road[i].col) { return 1; } } road[1].row=headrow;road[1].col=headcol; } return 0;}int main(){ char dir[120]; int n; while(scanf("%d",&n)!=EOF&&n) { int i,mark,flag=0,j; scanf("%s",dir); j=11; for(i=20;i>=1;i--) { road[i].row=25; road[i].col=j++; } for(i=0;i<strlen(dir);i++) { mark=move_and_judge(dir[i]); if(mark>0) { printf("The worm ran into itself on move %d.\n",i+1); flag=1; break; } else if(mark<0) { printf("The worm ran off the board on move %d.\n",i+1); flag=1; break; } } if(flag==0) { printf("The worm successfully made all %d moves.\n",strlen(dir)); } } return 0;}
0 0
- zoj1056
- zoj1056 The Worm Turns
- ZOJ1056 The Worm Turns
- ZOJ1056 The Worm Turns
- 树的直径最长路证明
- [HDU1724]Ellipse(辛普森积分)
- XCode 构建基于Swift的FrameWork
- Qt程序打包发布方法(使用官方提供的windeployqt工具)
- perl get请求加请求头
- zoj1056
- Redis介绍以及安装(Linux)
- C++拷贝构造函数详解
- MyBatis学习五:延迟加载
- mogilefs 的常见问题
- 如何在Ubuntu 14.04上使用NSD——一套仅权威DNS服务器
- verilog中parameter/defparam的用法
- 关于popToViewController用法 (有点疑惑,求助攻!!!)
- SwipeRefreshLayout完美实现ListView网络数据上拉加载以及下拉刷新