Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我泪流满面 等于八皇后怎么会出不来

Posted by vince4053040 at 2010-02-20 20:48:48 on Problem 1321
In Reply To:等于八皇后 Posted by:vince4053040 at 2010-02-20 20:10:06
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;

char map[8][8];
bool visit_R[8];
bool visit_C[8];
int k, sum, N;

void dfs(int i)
{
	int j;
	for(j = 0; j < N; j ++) {
		if(i >= 0 && i < N && map[i][j] == '#' && visit_C[j] == 0 && visit_R[i] == 0) {
			visit_R[i] = 1;
			visit_C[j] = 1;
			map[i][j] = '.';
			if(i == k)
				sum ++;
			else
				dfs(i + 1);

			visit_R[i] = 0;  //回溯
			visit_C[j] = 0;
			map[i][j] = '#';
		}
	}
	return ;
}

int main()
{
	int i, j;
	while(cin >> N >> k && N != -1) { 
		for(i = 0; i < N; i ++)
			for(j = 0; j < N; j ++)
				cin >> map[i][j];
		memset(visit_R, 0, sizeof(visit_R));
		memset(visit_C, 0, sizeof(visit_C));

		sum = 0;
		dfs(0);
		cout << sum << endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator