Portal: http://acm.hdu.edu.cn/showproblem.php?pid=1045
Test instructions
4.X ... Xx......
Give you the N*n, ' X ' represents the wall, fill in the black dots in the graph, there are no two points in a row (or column) that can be directly connected (that is: a row (column) of any two points there are walls apart.
Idea: Because of the n<=4, so the direct binary enumeration. Then use the check (int cur) function to determine if it is feasible.
It is said that the problem is greedy ....
Code:
/************************************************* author:ac_sorry* file:* Create date:* motto:one Heart one life* CSDN : http://blog.csdn.net/code_or_code*************************************************/#include <iostream># include<cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <cstdlib > #include <vector> #include <string> #include <utility> #include <map> #include <set># include<queue> #include <stack> #define INF 0x3f3f3f3f#define MOD 1000000007#define seed_ 131#define EPS 1e-8 #define MEM (A, B) memset (a,b,sizeof a) #define W (i) tree[i].w#define ls (i) tree[i].ls#define rs (i) tree[i].rsusing Namespace Std;typedef long Long ll;const int N=100010;char gra[10][10];int vis[10][10];int n;int Check (int cur) {int cn t=0; MEM (vis,0); for (int i=0;i<n;i++) {for (int j=0;j<n;j++) {if (gra[i][j]== ' X ') vis[i][j]=2; }} for (int i=0;i<n*n;i++) {if (cur& (1<<i)) {if (vis[i/n][i%n]) return-1; cout<<i<< "-" <<endl; Vis[i/n][i%n]=1; cnt++; }} int flag; for (int i=0;i<n;i++) {flag=0; for (int j=0;j<n;j++) {if (vis[i][j]==1) {if (flag) retur n-1; flag=1; } else if (vis[i][j]==2) flag=0; }} for (int j=0;j<n;j++) {flag=0; for (int i=0;i<n;i++) {if (vis[i][j]==1) {if (flag) retur n-1; flag=1; } else if (vis[i][j]==2) flag=0; }} return cnt;} int main () {while (scanf ("%d", &n) ==1) {if (n==0) break; for (int i=0;i<n;i++) scanf ("%s", Gra[i]); int ans=0; for (int i=0;i< (1<< (n*n)); i++) { Ans=max (Ans,check (i)); cout<<i<< "--\n"; } printf ("%d\n", ans); } return 0;}
hdu_1045 Fire Net