UVA12113(暴力+回溯)

就是個水題。
純暴力+回溯就ok了
只是題目看起來嚇人
處理好細節就行
關於覆蓋沒仔細看導致我一直寫出BUG orz


#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f3f
#define mod 1000000007
const int maxn = 1000000 + 5;
using namespace std;
char s[10][10];
int ans[10][10];
int maps[10][10];
void putmap(int op){
    int row = op / 3;
    int col = 2 * (op % 3);
    maps[row][col+1] = maps[row][col+3] = 2;
    maps[row+2][col+1] = maps[row+2][col+3] = 2;
    maps[row+1][col] = maps[row+1][col+4] = 1;
    maps[row+2][col] = maps[row+2][col+4] = 1;
    maps[row+2][col+2] = 0;
    maps[row+1][col+1] = maps[row+1][col+2] = maps[row+1][col+3] = 0;
}
bool judge(){
    for(int i=0; i<5; i++){
        for(int j=0; j<9; j++){
            if(ans[i][j] != maps[i][j])
                return false;
        }
    }
    return true;
}
bool dfs(int step){
    if(step > 6) return false;
    for(int i=0; i<9; i++){
        int remaps[5][10];
        for(int k=0; k<5; k++)
            for(int j=0; j<9; j++){
                remaps[k][j] = maps[k][j];
            }
        putmap(i);
        if(judge()) return true;
        if(dfs(step+1)) return true;
        for(int k=0; k<5; k++)
            for(int j=0; j<9; j++){
                maps[k][j] = remaps[k][j];
            }
    }
    return false;
}
int main(){
    int kases = 1;
    while(gets(s[0])){
        memset(ans, 0, sizeof(ans));
        memset(maps, 0, sizeof(maps));
        if(s[0][0] == '0') break;
        for(int i=1; i<5; i++)
            gets(s[i]);
        for(int i=0; i<5; i++){
            for(int j=0; j<9; j++){
                if(s[i][j] == '|')
                    ans[i][j] = 1;
                else if(s[i][j] == '_')
                    ans[i][j] = 2;
            }
        }
       printf("Case %d: ",kases++);
        if(dfs(1)) printf("Yes\n");
        else printf("No\n");
    }
}
/*
        #
 _ _ _  #
| |_ _| #
|_|   | #
  |_ _| #
*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章