POJ 1321 Board Problem DFS difficulty: 0

Source: Internet
Author: User

http://poj.org/problem?id=1321

Watch out for the pieces in the ' # ' place.

Matrix size But 8*8, even 8! The time complexity is enough to withstand, can be directly DFS Solver

The rows and columns at the current point of the label are accessed at DFS, and a new point is searched that has not been accessed by the column, note that the rows and columns of the current point are not accessed after the search is complete

#include <cstdio> #include <cstring>using namespace std;int n,k;char maz[8][9];int e[8][8],len[8];bool r[8] , c[8];void dye (int x,int y) {r[x]=c[y]=true;} void Undye (int x,int y) {r[x]=c[y]=false;}        int dfs (int s,int cnt) {if (cnt<=0) return 1;        int ans=0; for (int i=s+1;cnt+i<=n;i++) {for (int j=0;j<len[i];j++) {if (r[i]| |                        C[E[I][J]]) continue;                        Dye (i,e[i][j]);                        Ans+=dfs (i,cnt-1);                Undye (I,e[i][j]); }} return ans; int main () {while (scanf ("%d%d", &n,&k) ==2&&n!=-1) {for (int i=0;i<n;i++) scanf ("%s",                Maz[i]);                memset (len,0,sizeof (len));                Memset (r,0,sizeof (R));                Memset (C,0,sizeof (c)); for (int i=0;i<n;i++) {for (int j=0;j<n;j++) {if (maz[i][j]== ' # ')                  {                      E[i][len[i]++]=j;                        }}} if (k==0) {puts ("0");                Continue                } int ans=0; for (int i=0;k+i<=n;i++) {for (int j=0;j<len[i];j++) {dye (i,e[i]                                [j]);                                Ans+=dfs (i,k-1);                        Undye (I,e[i][j]);        }} printf ("%d\n", ans); } return 0;}

  

POJ 1321 Board Problem DFS difficulty: 0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.