【DFS 回溯】HDU 1045 Fire Net

来源:互联网 发布:疯狂粤语 粤知一二 编辑:程序博客网 时间:2024/05/16 07:13

题目链接:点这里

数据量比较小,可以直接深搜水过。

#include <stdio.h>#include <math.h>#include <string.h>#include <algorithm>using namespace std;char s[10][10];int dirx[] = {0,0,-1,1};int diry[] = {1,-1,0,0};int n, maxx = -0x3f3f3f3f;bool fit(int xx, int yy){    if(xx >= 0 && xx < n)        if(yy >= 0 && yy < n)            if(s[xx][yy] != 'X')            return true;    return false;}bool isok(int x, int y){    for(int i = 0; i < 4; i ++){        int newx = x, newy = y;        while(1){            newx = newx + dirx[i], newy = newy + diry[i];            if(!fit(newx, newy)) break;            if(s[newx][newy] == 'D') return false;        }    }    return true;}void dfs(int cnt){    if(cnt > maxx) maxx = cnt;    for(int i = 0; i < n; i ++){        for(int j = 0; j < n; j ++){            if(s[i][j] == '.' && isok(i, j)){                s[i][j] = 'D';                dfs(cnt + 1);                s[i][j] = '.';            }        }    }}int main(){    while(scanf("%d", &n) != EOF && n){        maxx = -0x3f3f3f3f;        memset(s, 0, sizeof(s));        getchar();        for(int i = 0; i < n; i ++)            gets(s[i]);        dfs(0);        printf("%d\n", maxx);    }    return 0;}



0 0
原创粉丝点击