TOJ 3973 Maze Again && TOJ 3128 简单版贪吃蛇
TOJ3973传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3973
时间限制(普通/Java):1000MS/3000MS 内存限制:65536KByte
总提交: 198 测试通过:52
描述
The maze is the same as problem D, and I strongly recommend you solve the previous one first because it.s easier than this.
This time, we want you design the command for our poor robot to move from where it is to its destination. The command sequence.s length should be the shortest. If several solutions exist, find the lexicographically minimum one.
Lexicographical sequence is the order in one dictionary. For example, “cat” is less than “do”, and “do” is less than “dog”.
输入
The first line contains a single integer T, indicating the number of test cases.
Each test case begins with one integer N (1 <= N <= 50), indicating the size of the maze. The followed N lines are N strings whose length is also N, indicating the maze.
输出
For each case, output a command string if there is a solution, otherwise output -1.
样例输入
2
2
S.
#T
4
S#..
.#T.
.##.
....
样例输出
RD
DDDRRRUUL
思路:入门广搜,从S出发到T,如果找得到路径输出字典序最小的那条,找不到则输出-1,可以在结构体内封装一个string属性,在向四个方向搜索的时候,加上对应字的母即可。本题有个小细节是,需要输出的是字典序最小的路径,这点区别于TOJ3128(简单版贪吃蛇),可以在写方向数组的时候,按四个方向的字典序从小到大写,int go[4][2] = {1,0,0,-1,0,1,-1,0}; 这样就可以在找到T的时候直接输出字符串。
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#define LL long long
#include<assert.h>
using namespace std;
int go[][] = {,,,-,,,-,};
char ma[][];
int vis[][];
struct note{
int x,y;
string s;
}pos,q;
int n;
int check(int x,int y){
if(x < ||x >= n || y < || y >= n)return ;
return ;
}
void bfs(int x,int y,int ex,int ey){
string str[];
int ans = ;
vis[x][y] = ;
queue<note>que;
pos.s = "";
pos.x = x;
pos.y = y;
que.push(pos);
while(que.size()){
q = que.front();
que.pop();
if(q.x == ex && q.y == ey){
cout<<q.s<<endl;return;
}
for(int i = ; i < ; i++){
int dx = q.x + go[i][];
int dy = q.y + go[i][];
if(check(dx,dy) && !vis[dx][dy] && ma[dx][dy] != '#'){
//cout<<q.s<<endl;
pos.x = dx;
pos.y = dy;
if(i == ) pos.s = q.s + "D";
if(i == ) pos.s = q.s + "L";
if(i == ) pos.s = q.s + "R";
if(i == ) pos.s = q.s + "U";
que.push(pos);
vis[dx][dy] = ;
}
}
}
puts("-1");
return;
}
int main(){
int t;
for(scanf("%d",&t);t--;){
int sx,sy,ex,ey;
memset(vis,,sizeof(vis));
scanf("%d",&n);
for(int i = ; i < n ; i++){
scanf("%s",ma[i]);
for(int j = ; j < n ;j ++){
if(ma[i][j] == 'S'){
sx = i;sy = j;
}
if(ma[i][j] == 'T'){
ex = i;ey = j;
}
}
}
bfs(sx,sy,ex,ey);
}
}
TOJ3128传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3128
总提交: 678 测试通过:202 Special Judge
现在我们来简化蛇的身体,假设初始化的时候蛇的身体只有一个头而已(呵,当然是假设的),那么蛇去吃食物的时候就不必考虑碰到自己的身体了。
例:
5 5
.....
S....
###.#
E....
#####
那么从S到E最短的走法是EEESSWWW。说明:N(north),S(south),W(west),E(east)。如果吃不到食物就输出Can't eat it!
注意:路径是最短的走的。
输入
输入数据有多组,每组输入的第一行是两个正整数R,C,表示行和列,3=<R,C<=100,下面输入R行C列的矩阵。
输入保证合法。
输出
每行输出最短的走法。
样例输入
5 5
.....
S....
###.#
E....
#####
样例输出
EEESSWWW
思路:这题也是广搜的入门题,也是对S开始进行广度优先搜素。因为是特判题,所以不需要考虑字典序,注意找不到的时候输出字符串"Can't eat it!"即可。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#define LL long long
#include<assert.h>
using namespace std;
int go[][]={{,},{,-},{-,},{,}};
char ma[][];
int vis[][];
struct note{
int x,y;
string s;
}pos,q;
int n,m;
int check(int x,int y){
if(x < ||x >= n || y < || y >= m)return ;
return ;
}
void bfs(int x,int y,int ex,int ey){
vis[x][y] = ;
queue<note>que;
pos.s = "";
pos.x = x;
pos.y = y;
que.push(pos);
while(que.size()){
q = que.front();
que.pop();
if(q.x == ex && q.y == ey){
cout<<q.s<<endl;
return;
}
for(int i = ; i < ; i++){
int dx = q.x + go[i][];
int dy = q.y + go[i][];
if(check(dx,dy) && !vis[dx][dy] && ma[dx][dy] != '#'){
//cout<<q.s<<endl;
pos.x = dx;
pos.y = dy;
if(i == ) pos.s = q.s + "E";
if(i == ) pos.s = q.s + "W";
if(i == ) pos.s = q.s + "N";
if(i == ) pos.s = q.s + "S";
que.push(pos);
vis[dx][dy] = ;
}
}
}
puts("Can't eat it!");
return;
}
int main(){
int t;
while(~scanf("%d %d",&n,&m)){
int sx,sy,ex,ey;
memset(vis,,sizeof(vis));
for(int i = ; i < n ; i++){
scanf("%s",ma[i]);
for(int j = ; j < m ;j ++){
if(ma[i][j] == 'S'){
sx = i;sy = j;
}
if(ma[i][j] == 'E'){
ex = i;ey = j;
}
}
}
bfs(sx,sy,ex,ey);
}
}
TOJ 3973 Maze Again && TOJ 3128 简单版贪吃蛇的更多相关文章
- OC版贪吃蛇
昨天写了一个js版贪吃蛇,今天突然想写一个OC版的,来对比一下两种语言的区别 oc版功能,适配所有尺寸iphone,可暂停,可设置地图和蛇的比例,可加速 对比一下会发现js版的相对OC版的会简单一些, ...
- JavaScript 实现简易版贪吃蛇(Day_13)
时光永远在变迁,你始终要丢下过去. 使用语言 JavaScript 概述 运用JavaScript 实现简易版<贪吃蛇>. Html 页面 1 <!DOCTYPE htm ...
- C#简单实现贪吃蛇程序(LinQ + Entity)
做梦想起来的C#简单实现贪吃蛇程序(LinQ + Entity) 最近一直在忙着单位核心开发组件的版本更新,前天加了一个通宵,昨天晚上却睡不着,脑子里面突然不知怎的一直在想贪吃蛇的实现方法.以往也有类 ...
- Java一个简单的贪吃蛇
Java一个简单的贪吃蛇 虽然GUI已经要淘汰了,但是手动写写界面还是有助于理解语法的,像构造函数 ,函数调用,内部类,继承,接口.有助于半初学者强化理解. 直接上代码 游戏主体类: package ...
- 如何用python制作贪吃蛇以及AI版贪吃蛇
用python制作普通贪吃蛇 哈喽,大家不知道是上午好还是中午好还是下午好还是晚上好! 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很 ...
- GUI简单实战——贪吃蛇
将前面学到的GUI基础知识完成实战,完成一个简单的贪吃蛇项目 项目功能 用键盘上下左右实现贪吃蛇的自动移动 贪吃蛇吃到食物后,长度加一,分数加一 贪吃蛇吃到自己的身体,则游戏结束 按空格键实现游戏的暂 ...
- js版贪吃蛇
之前没有写博客的习惯,这是我的第一个博客,有些的不好的地方,希望大家多多提意见 js版的贪吃蛇相对比较简单,废话不多说直接上代码,有需要注意的地方我会标红,github源码地址https://gith ...
- 做梦想起来的C#简单实现贪吃蛇程序(LinQ + Entity)
最近一直在忙着单位核心开发组件的版本更新,前天加了一个通宵,昨天晚上却睡不着,脑子里面突然不知怎的一直在想贪吃蛇的实现方法.以往也有类似的情况,白天一直想不通的问题,晚上做梦有时会想到更好的版本,于是 ...
- JavaScript版—贪吃蛇小组件
最近在学习JavaScript,利用2周的时间看完了<JavaScript高级编程>,了解了Js是一门面向原型编程的语言,没有像C#语言中的class,也没有私有.公有.保护等访问限制的级 ...
随机推荐
- 使用STM32CubeMX生成RTC工程[秒中断]
现在我们在之前的工程(http://www.cnblogs.com/libra13179/p/7170791.html)中修改 /** ******************************** ...
- VC 字符串转化和分割
原文:点击这里. 备忘:为了适用于Unicode环境,要养成使用_T()宏的习惯 1.格式化字符串 CString s;s.Format(_T("The num is %d."), ...
- 机器学习进阶-阈值与平滑-图像平滑操作(去噪操作) 1. cv2.blur(均值滤波) 2.cv2.boxfilter(方框滤波) 3. cv2.Guassiannblur(进行高斯滤波) 4. cv2.medianBlur(进行中值滤波)
1.cv2.blur(img, (3, 3)) 进行均值滤波 参数说明:img表示输入的图片, (3, 3) 表示进行均值滤波的方框大小 2. cv2.boxfilter(img, -1, (3, ...
- matt cutts : try something new for 30 days
30 天尝试新事物matt cutts : try something new for 30 days[小计划帮你实现大目标] 是否有些事情, 你一直想去做, 但就是没有实现?马特 ?卡茨建议: 尝试 ...
- Linux grep命令使用方法
Linux系统中grep命令可以根据指定的字符串或者正则表达式对文件内容进行匹配查找.在Linux文件处理和SHELL编程中使用广泛. grep基本语法 用法: grep [选项] "字符串 ...
- fwrite()中参数含义——size和count经常用搞反
函数原型:size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream); 注意:这个函数以二进制形式对文件进 ...
- TensorFlow 辨异 —— tf.add(a, b) 与 a+b(tf.assign 与 =)、tf.nn.bias_add 与 tf.add(转)
1. tf.add(a, b) 与 a+b 在神经网络前向传播的过程中,经常可见如下两种形式的代码: tf.add(tf.matmul(x, w), b) tf.matmul(x, w) + b 简而 ...
- 设置HTML编码为UTF-8
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- is 和 as 运算符
is和as运算符:is是判断是否是某个类型,返回true或falseo as Ren: 如果转换成功了,没问题:as 是用来转换如果没转换成功,不会报出错误,而是返回一个null值 例 实例化一个集合 ...
- linux下进程查找和杀死
比如杀死进程中叫 abc的进程 1.ps -ef |grep abc|awk '{print $2}' xargs kill -9 2.kill `pidof abc` `位于tab键 ...