Title Link: Checkerboard Issues
Parsing: DFS violence searches from top to bottom, from left to right.
AC Code:
Code 1#include <iostream> #include <cstdio> #include <cstring>using namespace Std;int N, K, Ans;char Maze[10][10];bool vis[10][10];void dfs (int x, int y, int step) {if (step = = k) {ans + +; return; } for (int i=x, i<n; i++) for (int j= (i = = x y:0); j<n; J + +) {if (!vis[i][j] && maze[i ][J] = = ' # ') {bool used[10][10]; Memset (used, false, sizeof (used)); for (int p=0; p<n; p++)//save tag for (int q=0; q<n; q++) used[p][q] = vis[ P][Q]; for (int p=0; p<n; p++) {vis[i][p] = true; VIS[P][J] = true; } dfs (I, J, step+1); for (int p=0; p<n; p++)//Mark restore for (int q=0; q<n; q++) vis[p][q] = used[ P][Q]; }}}int Main () {//Freopen ("In.txt", "R", stdin); while (scanf ("%D%d ", &n, &k) = = 2) {if (n = =-1 && k = =-1) break; for (int i=0, i<n; i++) for (int j=0; j<n; j + +) cin>>maze[i][j]; Ans = 0; Memset (Vis, false, sizeof (VIS)); DFS (0, 0, 0); printf ("%d\n", ans); } return 0;}
Code 2#include <iostream> #include <cstdio> #include <cstring>using namespace Std;int N, K, Ans;char Maze[10][10];bool vx[10], vy[10];void dfs (int x, int y, int step) { if (step = k) { ans + +; return; } for (int i=x, i<n; i++) for (int j= (i = = x y:0); j<n; J + +) { if (!vx[i] &&!vy[j] && Maze[i] [j] = = ' # ') { vx[i] = true; Mark Vy[j] = true; DFS (I, J, step+1); Vx[i] = false; Mark Restore vy[j] = false;}}} int main () {// freopen ("In.txt", "R", stdin); while (scanf ("%d%d", &n, &k) = = 2) { if (n = =-1 && k = =-1) break; for (int i=0; i<n; i++) scanf ("%s", Maze[i]); Ans = 0; MEMSET (VX, FALSE, sizeof (VX)); Memset (VY, False, sizeof (VY)); DFS (0, 0, 0); printf ("%d\n", ans); } return 0;}
Copyright NOTICE: This article is sxk original article, reprint please attach this article link ^_^
POJ 1321 Checkerboard Problem (DFS)