Description
由于牛奶市场的需求,奶牛必须前往城市,但是唯一可用的交通工具是出租车.教会奶牛如何在城市里打的.
给出一个城市地图,东西街区E(1≤E≤40),南北街区N(1≤N≤30).制作一个开车指南给出租车司机,告诉他如何从起点(用S表示)到终点(用E表示).每一个条目用空格分成两部分,第一个部分是方向(N,E,S,W之一),第二个是一个整数,表示要沿着这个方向开几个十字路口.如果存在多条路线,你应该给出最短的.数据保证,最短的路径存在且唯一.地图中“+”表示十字路口,道路用“I”和“一”表示.建筑和其他设施用“.”表示.下面是一张地图:
出租车可以沿着东,北,西,北,东开两个十字路口,以此类推.具体将由样例给出。
Input
第1行:两个用空格隔开的整数N和E.
第2到2N行:每行有2E-I个字符,表示地图.
Output
Sample Input
3 6+-+-+.+-+-+|...|.....|+-+.+-+-+-+..|.......|S-+-+-+.E-+
我有个同学2017gdgzoi999用深搜做出来了,看深搜代码的可以看他的博客【广搜】城市交通。
我考试的时候用深搜做了38分,稍微改一下变成50分,再改改直接Accepted!
不多说,直接看代码:
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int n, m, startx, starty, ans, anses[2][30000],
xx[4] = {0, 1, -1, 0},
yy[4] = {1, 0, 0, -1};
char s[100][100],
di[4] = {'E', 'S', 'N', 'W'};
bool tf = false;
void dfs(int sx, int sy)
{
if(tf == true) return;
if(sx < 0 || sx >= n || sy < 0 || sy >= m || s[sx][sy] == '.') return;
//cout << "sx: " << sx << " sy: " << sy << endl;
if(s[sx][sy] == 'E')
{
//cout << "Here has a E!!!" << endl;
//cout << "sx: " << sx << " sy: " << sy << endl;
//cout << "ans: " << ans << endl;
//cout << "anses: ";
for(int x = ans-1; x > 0; x--) if(anses[0][x] == anses[0][x-1])
{
anses[1][x-1] += anses[1][x];
anses[0][x] = -1;
}
for(int x = 0; x < ans; x++) if(anses[0][x] != -1) cout << di[anses[0][x]] << " " << anses[1][x] << endl;
cout << endl;
tf = true;
return;
}
for(int x = 0; x < 4; x++)
{
char l = s[sx][sy];
s[sx][sy] = '.';
anses[0][ans] = x;
anses[1][ans] = 1;
if(s[sx + xx[x]][sy + yy[x]] == '+' || s[sx + xx[x]][sy + yy[x]] == 'E') ans++;
dfs(sx + xx[x], sy + yy[x]);
s[sx][sy] = l;
anses[0][ans] = -1;
anses[1][ans] = 1;
if(s[sx + xx[x]][sy + yy[x]] == '+' || s[sx + xx[x]][sy + yy[x]] == 'E') ans--;
}
}
int main()
{
for(int x = 0; x < 30000; x++) anses[0][x] = -1;
cin >> n >> m;
n = n*2-1; m = m*2-1;
for(int x = 0; x < n; x++)
for(int y = 0; y < m; y++)
{
cin >> s[x][y];
if(s[x][y] == 'S')
{
startx = x;
starty = y;
}
}
//cout << "Here!" << endl;
dfs(startx, starty);
return 0;
}
写得不算太丑,0ms通过,还是挺好的。
老师讲的时候说的广搜,同学用广搜,网上全是广搜,但是我就能用深搜做出来!