DFS&BFS

Overview


These two are two important search algorithm to deal with Graph Theory.

In detail, because of the feature of these two algorithm, BFS is often used with queue, and DFS is often used with stack.

OJ Practise


POJ 1101


Use BFS

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
using namespace std;

const int maxe= 75+3;
const int INF= 0x3f3f3f3f;
typedef pair<int , int> P;
typedef pair<P, P> PP;
int dx[4]= {1, 0, -1, 0}, dy[4]= {0, 1, 0, -1};
class BFS{
	int w, h;
	int bd, pi;
	int x1, y1, x2, y2;
	bool bod[maxe][maxe];
	int d[maxe][maxe];
	int chag;
	char path;
	bool flag;
	queue<PP> Q;
	void Init()
	{
		memset(bod, 0, sizeof(bod));
		flag= false;
		pi= 0;
	}
	bool InBoard(int x, int y)
	{
		return -1< x && w+2> x && -1< y && h+2> y;
	}
	bool bfs()
	{
		memset(d, 0x3f, sizeof(d));
		Q.push(PP(P(x1, y1), P(0, 0)));
		d[x1][y1]= 0;

		while (Q.size()){
			P tmp= Q.front().first;
			P tc= Q.front().second;
			Q.pop();

			if (tmp.first== x2 && tmp.second== y2){
				chag= tc.second;
				return true;
			}

			for ( int i= 0; i< 4; ++i){
				int nx= tmp.first+ dx[i];
				int ny= tmp.second+ dy[i];
				if (InBoard(nx, ny) && !bod[nx][ny] && INF== d[nx][ny]){
					if (tc.first!= i+1){
						chag= tc.second+1;
					}
					else{
						chag= tc.second;
					}
					Q.push(PP(P(nx, ny), P(i+1, chag)));
					d[nx][ny]= d[tmp.first][tmp.second]+1;
				}
			}
		}

		return false;
	}
	void PrintAns()
	{
		if (flag){
			printf("Pair %d: %d segments.\n", ++pi, chag);
		}
		else{
			printf("Pair %d: impossible.\n", ++pi);
		}
	}
public:
	void Solve()
	{
		bd= 0;
		while (EOF!= scanf("%d %d", &w, &h) && (w || h)){
			Init();
			for (int i= 1; i< h+1; ++i){
				getchar();
				for (int j= 1; j< w+1; ++j){
					path= getchar();
					if (' '!= path){
						bod[j][i]= true;
					}
				}
			}
			printf("Board #%d:\n", ++bd);
			while (EOF!= scanf("%d %d %d %d", &x1, &y1, &x2, &y2)
			 && (x1 || y1 || x2 || y2)){
				bod[x2][y2]= false;
				queue<PP> empty;
				swap(empty, Q);
				flag= bfs();
				bod[x2][y2]= true;
				PrintAns();
			}
			putchar('\n');
		}
	}
};

int main()
{
	BFS poj1101;
	poj1101.Solve();

	return 0;
}

POJ 1321


It's the deformation of the famous problem n-queen. Use DFS.

#include <iostream>
#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;

const int maxn= 10;

typedef long long ll;
class DFS{
	int n, k;
	int depth;
	ll ans;
	char bod[maxn][maxn];
	bool Lin[maxn];
	int on;
	void Init()
	{
		memset(bod, 0, sizeof(bod));
		depth= 0;
		memset(Lin, 0, sizeof(Lin));
		on= 0;
		ans= 0;
	}
	void dfs(int s)
	{
		++depth;
		if (k-on-1> n-depth){
			--depth;
			return;
		}
		if (k== on){
			--depth;
			++ans;
			return;
		}

		for (int i= 0; i< n; ++i){
			if ('#'== bod[s][i] && !Lin[i]){
				++on;
				Lin[i]= true;
				dfs(s+1);
				Lin[i]= false;
				--on;
			}
		}
		dfs(s+1);
		--depth;
	}
public:
	void Solve()
	{
		while (EOF!= scanf("%d %d", &n, &k) && (-1!= n || -1!= k)){
			Init();
			for (int i= 0; i< n; ++i){
				cin>>bod[i];
			}
			dfs(0);
			cout<<ans<<endl;
		}
	}
};

int main()
{
	DFS poj1321;
	poj1321.Solve();

	return 0;
}
posted @ 2019-12-09 17:09  IdiotNe  阅读(75)  评论(0)    收藏  举报