Ural1298 Knight 经典DFS

这个题目很想当时刚开始学BFS时所做的一道题目,我记得是POJ,的也是马走日

这题目就是给了你一个n * n的棋盘,从(0,0)点出发,马走日的方式,是否可以将棋盘走遍,而且每个格子只能走一次

那天先是写了bfs,但是记录方式开了个三维的,最后超时,没办法改为dfs,然后就是一直WA,或者RE实在不明白是为什么,补题已经隔了两天了,我实在没有好的办法啊,最后又敲了一次,发现过了!找 之前的代码 发现就是dir数组不一样,真奇葩,就是 走有八个方向,顺序不一样 会WA?神奇的题目

做法简单,直接DFS并标记 即可,用一个容器来记录路劲,最后发现 走到步数为 n*n就算是完成了

int n;

int dir[8][2]={2,1,1,2,-1,2,-2,1,-2,-1,-1,-2,1,-2,2,-1};

bool vis[10][10];

vector<pair<int ,int > > G;

int mark;

int tot;

void init() {
	memset(vis,false,sizeof(vis));
	mark = 0;
	G.clear();
}

bool input() {
	while(cin>>n) {

		return false;
	}
	return true;
}

int dfs(int x,int y,int step) {
	if(step == n * n){mark = 1;return step;}
	for(int i=0;i<8;i++) {
		int dx = x + dir[i][0];
		int dy = y + dir[i][1];
		if(dx < 0 || dx >= n || dy < 0 || dy >= n)continue;
		if(vis[dx][dy])continue;
		G.push_back(make_pair(dx,dy));
		vis[dx][dy] = true;
		dfs(dx,dy,step + 1);
		if(mark)return G.size();
		G.pop_back();
		vis[dx][dy] = false;
	}
	return 0;
}

void cal() {
	G.push_back(make_pair(0,0));
	tot++;
	vis[0][0] = true;
	dfs(0,0,1);
	if(!mark){puts("IMPOSSIBLE");return ;}
	for(int i=0;i<G.size();i++)
		printf("%c%d\n",G[i].first + 'a',G[i].second + 1);
}

void output() {

}

int main() {
	while(true) {
		init();
		if(input())return 0;
		cal();
		output();
	}
	return 0;
}
时间: 2024-08-06 12:43:10

Ural1298 Knight 经典DFS的相关文章

HDU 1016 Prime Ring Problem --- 经典DFS

思路:第一个数填1,以后每个数判断该数和前一个数想加是否为素数,是则填,然后标记,近一步递归求解. 然后记得回溯,继续判断下一个和前一个数之和为素数的数. /* HDU 1016 Prime Ring Problem --- 经典DFS */ #include <cstdio> #include <cstring> int n; bool primer[45], visit[25]; //primer打素数表,visit标记是否访问 int a[25]; //数组a存储放置的数 /

EOJ1981 || POJ1011 经典dfs+剪枝+奇怪的数据

题目:EOJ1981 || POJ1011   经典dfs+剪枝+奇怪的数据 Description George took sticks of the same length and cut them randomly until all partsbecame at most 50 units long. Now he wants to return sticks to the originalstate, but he forgot how many sticks he had origi

URAL 1298. Knight(DFS啊 )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1298 1298. Knight Time limit: 2.0 second Memory limit: 64 MB Even paratroopers have vacations. The flight to Sirius in the depths of "The Admiral Brisco" Leo Hao whiled away with chessboard.

经典DFS问题实践

八皇后问题: 1 //八皇后问题 经典的DFS问题实践 2 #include<iostream> 3 #include<cmath> 4 #include<algorithm> 5 #include<cstdio> 6 using namespace std; 7 int mat[8][8]; 8 int ans=0; 9 bool check(int row,int col)//最终检查满足要求,则return 1 10 { 11 for(int i=0;

LeetCode51 N皇后——经典dfs+回溯(三段式解法)

代码如下: 1 class Solution { 2 public: 3 // record[row] 该行对应的列 4 vector<vector<string> > ans; // 结果集 5 vector<vector<string>> solveNQueens(int n) { 6 string s = ""; 7 for(int i=0; i<n; i++){ 8 s += '.'; 9 } 10 vector<in

POJ 1564 经典dfs

1.POJ 1564 Sum It Up 2.总结: 题意:在n个数里输出所有相加为t的情况. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define F(i,a,b) for (int i=a;i<=b;i++) #define mes(a,b) memse

poj1564-Sum It Up(经典DFS)

给出一个n,k,再给出的n个数中,输出所有的可能使几个数的和等于k Sample Input 4 6 4 3 2 2 1 15 3 2 1 1400 12 50 50 50 50 50 50 25 25 25 25 25 250 0Sample Output Sums of 4:43+12+22+1+1Sums of 5:NONESums of 400:50+50+50+50+50+50+25+25+25+2550+50+50+50+50+25+25+25+25+25+25 明显的DFS,这个d

经典DFS问题 oilland 连通块

#include "iostream" #include "cstdio" using namespace std; int dir[8][2]={{0,1},{0,-1},{1,0},{-1,0},{1,-1},{-1,1},{1,1},{-1,-1}}; int count,r,c; char map[100][100]; void dfs(int x,int y)//深搜函数,参数为坐标(定位) { if(x<0||y<0||x>=r||y&

poj3984(经典dfs)

题目链接:http://poj.org/problem?id=3984 分析:直接深搜从起点到终点,如何取最短路线,其实只要优先向下或向右走即可. #include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <queue> #include <cstdlib> #include