【习题 7-10 Uva11214】Guarding the Chessboard

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

迭代加深搜索。 可以想见最后深度不会很深吧。。 然后皇后之间互相攻击到是允许的。。 就这样

【代码】

/*
  	1.Shoud it use long long ?
  	2.Have you ever test several sample(at least therr) yourself?
  	3.Can you promise that the solution is right? At least,the main ideal
  	4.use the puts("") or putchar() or printf and such things?
  	5.init the used array or any value?
  	6.use error MAX_VALUE?
  	7.use scanf instead of cin/cout?
  	8.whatch out the detail input require
*/
/*
    一定在这里写完思路再敲代码!!!
*/
#include <bits/stdc++.h>
using namespace std;

const int O = 50;
const int N = 100,M = 10;

int n,m;
int visx[N],visy[N],visc[M+10],visr[M+10];
char s[M+10][M+10];

int maxnum;

bool ok(){
    for (int i = 1;i <= n;i++)
        for (int j = 1;j <= m;j++)
            if (s[i][j]=='X' && !(visr[i] || visc[j]||visx[O+i+j]                                      || visy[O+i-j]))
                    return false;
    return true;
}

bool dfs(int num,int r,int c){
    if (num==maxnum){
        if (ok()) return true;
        return false;
    }

    if (c==m+1) return dfs(num,r+1,1);

    if (r>n) return false;

    if (dfs(num,r,c+1)) return true;

    visc[c]++;visr[r]++;
    visx[r+c+O]++; visy[r-c+O]++;

    if (dfs(num+1,r,c+1)) return true;

    visc[c]--;visr[r]--;
    visx[r+c+O]--; visy[r-c+O]--;

    return false;
}

int main(){
	#ifdef LOCAL_DEFINE
	    freopen("rush_in.txt", "r", stdin);
	#endif
	ios::sync_with_stdio(0),cin.tie(0);
	int kase = 0;
    while (cin >>n >> m && n &&m){
        memset(visx,0,sizeof visx);
        memset(visy,0,sizeof visy);
        memset(visr,0,sizeof visr);
        memset(visc,0,sizeof visc);
        for (int i = 1;i <= n;i++)
            cin >> (s[i]+1);
        for (maxnum = 0; ;maxnum++){
            if (dfs(0,1,1)) break;
        }
        cout <<"Case "<<++kase<<": "<<maxnum<<endl;
    }
	return 0;
}
posted @ 2018-01-02 15:19  AWCXV  阅读(265)  评论(0编辑  收藏  举报