POJ 1321 Checkerboard Problem (DFS)

Source: Internet
Author: User

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)

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.