Forrest


私信TA

用户名:dotcpp0717441

访问量:1656

签 名:

等  级
排  名 150
经  验 7094
参赛次数 1
文章发表 79
年  龄 0
在职情况 教师
学  校 优学乐程
专  业

  自我简介:

解题思路:DFS深搜 + 剪枝

注意事项:移动的顺序, 下右上左

参考代码:

#include<iostream>
#include<string>
#include<sstream>
#include<cstring>
using namespace std;
const int N = 1e1 + 10;
int g[N][N],dis[N][N],mx= 0x3f3f3f3f;
int d[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
bool v[N][N];
string m_path; 
void dfs(int x, int y, string path, int lth){
	if(x == 4 && y == 4){
		if(mx > lth) {
			m_path = path;
			mx = lth;
		}
		return;
	}
	if(dis[x][y] > lth) dis[x][y] = lth;
	else return;
	for(int i = 0 ; i < 4; i ++){
		int nx = d[i][0] + x;
		int ny = d[i][1] + y;
		if(nx >= 0 && nx < 5 && ny >=0 && ny < 5 && !g[nx][ny] && !v[nx][ny]){
			v[nx][ny] = true;
			stringstream ss;
			ss << path << "\n(" <<nx <<", "<<ny<<")";
			dfs(nx,ny,ss.str(),lth + 1);
			v[nx][ny] = false;
		} 
	}
	
}
int main()
{
	memset(dis, 0x3f, sizeof(dis));
	for(int i = 0; i < 5; i ++)
		for(int j = 0; j < 5; j ++)
			cin >> g[i][j];
	string s = "(0, 0)";
	dfs(0,0,s,0);
	cout << m_path;
	return 0;
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区