Simple Search Practice Backtracking
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <sstream>5#include <string>6#include <algorithm>7#include <list>8#include <map>9#include <vector>Ten#include <queue> One#include <stack> A#include <cmath> -#include <cstdlib> - using namespacestd; the intn,k; - intans; - Chars[Ten][Ten]; - intusex[Ten],usey[Ten]; + BOOLOkintXinty) { - if(x<0|| x>=n| | y<0|| y>=N) + return 0; A if(s[x][y]=='.'){ at return 0; - } - if(usex[x]==1|| usey[y]==1) - return 0; - return 1; - } in voidDfsintXinty) { - if(y==k) { toans++; + return ; - } the if(x>N) { * return ; $ }Panax Notoginseng for(intI=0; i<n;i++){ - if(OK (x,i)) { theusey[i]=1; +usex[x]=1; ADFS (x+1, y+1); theusey[i]=0; +usex[x]=0; - } $ } $DFS (x+1, y); - } - intMain () { the while(~SCANF ("%d%d", &n,&k), n!=-1|| k!=-1){ -ans=0;Wuyimemset (Usey,0,sizeof(Usey)); thememset (Usex,0,sizeof(Usex)); - for(intI=0; i<n;i++){ Wuscanf"%s", S[i]); - } AboutDfs0,0); $printf"%d\n", ans); - } - return 0; -}
View Code
POJ 1321 Checkerboard Problem Dfs Search