0
点赞
收藏
分享

微信扫一扫

T213258 DC逃跑解析

四月天2021 2022-04-02 阅读 9
c++

题目背景

众所周知, D C DC DC 是一所著名的学校。

D C DC DC是一堵墙,外面的人想进来,里面的想出去。

z q c zqc zqc 将你关进了 D C DC DC,为了防止你逃出去,就依 D C DC DC的地势,修建了许多墙壁,阻挡你的逃跑。

而你—— D C DC DC的一名被禁锢的学生,要逃出 z q c zqc zqc的禁锢,准备逃出 D C DC DC

题目描述

你将会从你的教室的位置逃往一个隐秘的虫洞,你可以从虫洞中逃出,外边就是 1 0 9 10^9 109光年外的你家。

输入格式

第一行是两个数,分别表示 D C DC DC的宽 ( m ) (m) (m) 和长 ( n ) (n) (n)

接下来是一个二维矩阵,是 D C DC DC的地图,其中 0 0 0表示墙, 1 1 1表示可走的路。而你的教室位置用 ‘^’ 表示,虫洞的位置用 ‘#’ 表示。

输出格式

你逃出 D C DC DC的所有路径,一行一条路径,路径用 ( x , y ) − > ( (x,y)->( (x,y)>(即坐标 ) ) )表示,最后一个位置不要 ‘ − > -> >’ 。

优先顺序为左上右下。

最后一行为总路径数,如果无法到达虫洞,就输出 " D C "DC "DC i s is is t o o too too s t r o n g strong strong ! " !" !"

输入输出样例

输入 #1

6 5
1 0 1 1 1
0 1 1 0 1
1 ^ 1 1 0
1 1 1 1 1
1 1 1 0 0
0 # 1 1 0

输出 #1

(3,2)->(3,1)->(4,1)->(4,2)->(4,3)->(5,3)->(5,2)->(6,2)
(3,2)->(3,1)->(4,1)->(4,2)->(4,3)->(5,3)->(6,3)->(6,2)
(3,2)->(3,1)->(4,1)->(4,2)->(5,2)->(5,3)->(6,3)->(6,2)
(3,2)->(3,1)->(4,1)->(4,2)->(5,2)->(6,2)
(3,2)->(3,1)->(4,1)->(5,1)->(5,2)->(4,2)->(4,3)->(5,3)->(6,3)->(6,2)
(3,2)->(3,1)->(4,1)->(5,1)->(5,2)->(5,3)->(6,3)->(6,2)
(3,2)->(3,1)->(4,1)->(5,1)->(5,2)->(6,2)
(3,2)->(2,2)->(2,3)->(3,3)->(3,4)->(4,4)->(4,3)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(6,3)->(6,2)
(3,2)->(2,2)->(2,3)->(3,3)->(3,4)->(4,4)->(4,3)->(4,2)->(4,1)->(5,1)->(5,2)->(6,2)
(3,2)->(2,2)->(2,3)->(3,3)->(3,4)->(4,4)->(4,3)->(4,2)->(5,2)->(5,3)->(6,3)->(6,2)
(3,2)->(2,2)->(2,3)->(3,3)->(3,4)->(4,4)->(4,3)->(4,2)->(5,2)->(6,2)
(3,2)->(2,2)->(2,3)->(3,3)->(3,4)->(4,4)->(4,3)->(5,3)->(5,2)->(6,2)
(3,2)->(2,2)->(2,3)->(3,3)->(3,4)->(4,4)->(4,3)->(5,3)->(6,3)->(6,2)
(3,2)->(2,2)->(2,3)->(3,3)->(4,3)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(6,3)->(6,2)
(3,2)->(2,2)->(2,3)->(3,3)->(4,3)->(4,2)->(4,1)->(5,1)->(5,2)->(6,2)
(3,2)->(2,2)->(2,3)->(3,3)->(4,3)->(4,2)->(5,2)->(5,3)->(6,3)->(6,2)
(3,2)->(2,2)->(2,3)->(3,3)->(4,3)->(4,2)->(5,2)->(6,2)
(3,2)->(2,2)->(2,3)->(3,3)->(4,3)->(5,3)->(5,2)->(6,2)
(3,2)->(2,2)->(2,3)->(3,3)->(4,3)->(5,3)->(6,3)->(6,2)
(3,2)->(3,3)->(3,4)->(4,4)->(4,3)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(6,3)->(6,2)
(3,2)->(3,3)->(3,4)->(4,4)->(4,3)->(4,2)->(4,1)->(5,1)->(5,2)->(6,2)
(3,2)->(3,3)->(3,4)->(4,4)->(4,3)->(4,2)->(5,2)->(5,3)->(6,3)->(6,2)
(3,2)->(3,3)->(3,4)->(4,4)->(4,3)->(4,2)->(5,2)->(6,2)
(3,2)->(3,3)->(3,4)->(4,4)->(4,3)->(5,3)->(5,2)->(6,2)
(3,2)->(3,3)->(3,4)->(4,4)->(4,3)->(5,3)->(6,3)->(6,2)
(3,2)->(3,3)->(4,3)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(6,3)->(6,2)
(3,2)->(3,3)->(4,3)->(4,2)->(4,1)->(5,1)->(5,2)->(6,2)
(3,2)->(3,3)->(4,3)->(4,2)->(5,2)->(5,3)->(6,3)->(6,2)
(3,2)->(3,3)->(4,3)->(4,2)->(5,2)->(6,2)
(3,2)->(3,3)->(4,3)->(5,3)->(5,2)->(6,2)
(3,2)->(3,3)->(4,3)->(5,3)->(6,3)->(6,2)
(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(5,3)->(6,3)->(6,2)
(3,2)->(4,2)->(4,1)->(5,1)->(5,2)->(6,2)
(3,2)->(4,2)->(4,3)->(5,3)->(5,2)->(6,2)
(3,2)->(4,2)->(4,3)->(5,3)->(6,3)->(6,2)
(3,2)->(4,2)->(5,2)->(5,3)->(6,3)->(6,2)
(3,2)->(4,2)->(5,2)->(6,2)
37

输入 #2

6 5
1 0 1 1 1 
0 1 1 0 1 
1 ^ 1 1 0 
0 0 1 1 1 
1 1 0 0 0 
0 # 1 1 0 

输出 #2

DC is too strong !

说明/提示

1 ≤ m , n ≤ 15 1 ≤ m,n ≤ 15 1m,n15

( m , n ∈ N + ) (m,n∈N_+) (m,nN+)

附件下载

T213258 DC逃跑_fj.zip 1.33KB

解析

简单的爆搜

注意两点:

1.如果没有解要输出 “DC is too strong !” 。

2.搜索前进的方向要遵循左上右下的优先顺序。

无注释代码 (其实是懒得写):

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cstdio>
using namespace std;
int m,n,sx,sy,ex,ey,k[101];
int dx[5]={0,0,-1,0,1},dy[5]={0,-1,0,1,0};
bool map[20][20],flag;
struct node{
	int x,y;
}p[100];
void print(int n){
	for(int i = 1; i < n; ++i) printf("(%d,%d)->",p[i].x,p[i].y);
	printf("(%d,%d)\n",p[n].x,p[n].y);
}
void dfs(int x,int y,int dep){
	if(x == ex && y == ey){
		flag = 1;
		print(dep-1);
	}
	for(int i = 1; i <= 4; ++i){
		if(map[x+dx[i]][y+dy[i]]){
			map[x+dx[i]][y+dy[i]] = 0;
			p[dep].x = x+dx[i];
			p[dep].y = y+dy[i];
			dfs(x+dx[i],y+dy[i],dep+1);
			map[x+dx[i]][y+dy[i]] = 1;
		}
	}
}
int main(){
	scanf("%d%d",&m,&n);
	char c;
	for(int i = 1; i <= m; ++i){
		for(int j = 1; j <= n; ++j){
			cin >> c;
			if(c == '1') map[i][j] = 1;
			else if(c == '0') map[i][j] = 0;
			else if(c == '^') sx = i,sy = j;
			else if(c == '#') ex = i,ey = j;
		}
	}
	map[sx][sy] = 0;
	map[ex][ey] = 1;
	p[1].x = sx;
	p[1].y = sy;
	dfs(sx,sy,2);
	if(!flag) printf("DC is too strong !\n");
	return 0;
}
举报

相关推荐

0 条评论