1. 程式人生 > >(dfs演算法) Fire Net

(dfs演算法) Fire Net

Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.

A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.

Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.

The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.

The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.


Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration.

InputThe input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integer n that is the size of the city; n will be at most 4. The next n lines each describe one row of the map, with a '.' indicating an open space and an uppercase 'X' indicating a wall. There are no spaces in the input file.
OutputFor each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration.
Sample Input
4
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
Sample Output
5
1
5
2
4

思路,簡單dfs回溯

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int n;
char a[5][5];
int vis[5][5];
int sum;
int judge(int r,int c)
{
    if(r<1||c<1||r>n||c>n)
        return 0;
    if(vis[r][c])
        return 0;
    for(int i=1;i<=n;++i)
        if(vis[i][c]==1)
    {
        if(i>r)
            swap(i,r);
        if(r-i==1)
            return 0;
        for(int j=i+1;j<r;++j)
            {
                if(vis[j][c]==2)
                    break;
                if(j==r-1&&vis[j][c]!=2)
                    return 0;
            }
    }
    for(int i=1;i<=n;++i)
        if(vis[r][i]==1)
    {
        if(i>c)
            swap(i,c);
        if(c-i==1)
            return 0;
        for(int j=i+1;j<n;++j)
            {
                if(vis[r][j]==2)
                    break;
                if(j==c-1&&vis[r][j]!=2)
                return 0;
            }
    }
    return 1;
}
void dfs(int r,int c,int cnt)
{
    if(judge(r,c))
    {
        vis[r][c]=1;
        cnt++;
       // printf("%d %d %d\n",r,c,cnt);
        sum=max(sum,cnt);
    }
    if(r==n&&c==n)
    {
        return ;
    }

   else if(c<n)
    {
        ++c;
    }
    else if(r<n&&c==n){
        ++r;
        c=1;
       // printf("%d %d\n",r,c);
    }
    int s=0;
    for(int i=r;i<=n;++i,++s)
        for(int j=s=0?c:1;j<=n;++j)
            if(judge(i,j))
    {
       // printf("%d %d\n",i,j);
        dfs(i,j,cnt);
        vis[i][j]=0;
    }

}
int main()
{
    while(cin>>n&&n){
        sum=0;
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;++i)
            scanf("%s",a[i]+1);
        for(int i=1;i<=n;++i)
            for(int j=1;j<=n;++j)
                if(a[i][j]=='X')
                    vis[i][j]=2;

        for(int i=1;i<=n;++i)
            for(int j=1;j<=n;++j)
                if(judge(i,j))
                {
                    dfs(i,j,0);
                    vis[i][j]=0;
                }
            printf("%d\n",sum);
    }
}


相關推薦

dfs演算法 Fire Net

Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing

2016屆藍橋杯牌型種類dfs演算法

牌型種數 小明被劫持到X賭城,被迫與其他3人玩牌。 一副撲克牌(去掉大小王牌,共52張),均勻發給4個人,每個人13張。 這時,小明腦

ZOJ 1457 Prime Ring Problemdfs+剪枝

line alt sent math ica scanf 素數 n) div ?? Prime Ring Problem Time Limit: 10 Seconds Memory Limit: 32768 KB A ring is compose o

HDU 3887 Counting OffspringDFS

pac ria div cst key rst n-1 include http Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java

如鵬網學習筆記十四ASP.NET

表單參數 form表單 web服務 exp 序列化 date 文字 arr 處理程序 Asp.net筆記 一、Socket類   進行網絡編程的類,可以在兩臺計算機之間進行網絡通訊   過程:     向服務器發送指令:     GET /index.html HTTP

搜索模板DFS/BFS

個數 while d+ nod div ++ int second code DFS int b[4][2] = {-1,0,0,1,1,0,0,-1}; int DFS( pair<int,int> x ) { int res=0; vis

POJ 3279 FliptileDFS+反轉

puts 時間復雜度 有一個 坐標 pos 上下左右 clas main target 題目鏈接:http://poj.org/problem?id=3279 題目大意:有一個n*m的格子,每個格子都有黑白兩面(0表示白色,1表示黑色)。我們需要把所有的格子都反轉成黑色,

Making Genome in Berland DFS+思維

put win types addition ecif pes set mini cati 個人心得:被這周的專題名坑了,一直用字典樹,明明題目看得很清楚了,不存在相同的字母,即每個字母最多只有一個直接後驅,那麽只要用DFS走開頭就好了, 思想很巧妙,用vector,記錄後

【UVa】Biggest Numberdfs+剪枝

scanf sin ret break puts 從大到小 如果 ssl ges 題目 題目 ? ? 分析 典型搜索,考慮剪枝。 統計一下聯通分量。 1、本位置能夠達到所有的點的數量加上本已有的點,還沒有之前的結果長,直接返回。 2、當本位置能夠達到所有的點的數量加上本

POJ 3083 -- Children of the Candy CornDFS+BFSTLE

cin 列數 tle 圖片 逆時針 sta div ima ostream POJ 3083 -- Children of the Candy Corn(DFS+BFS) 題意: 給定一個迷宮,S是起點,E是終點,#是墻不可走,.可以走 1)先輸出左轉優先時,從S到E的步數

Codeforces 931D Peculiar apple-treedfs+思維.cpp

str () target 是我 ces spa HR lan span 題目鏈接:http://codeforces.com/contest/931/problem/D 題目大意:給你一顆樹,每個節點都會長蘋果,然後每一秒鐘,蘋果往下滾一個。兩個兩個會抵消蘋果。問最後在

Skype for Business Client PPT 共享問題DFS環境

PPT共享錯誤 PPT共享失敗 PPT共享 文件存儲是DFS環境,在共享PPT時出現錯誤。問題表述:1.點對點之間PPT 共享出現以下錯誤,沒有上傳過程直接報錯。2.邀請人員開會,會議中共享PPT、白板、投票按鈕消失。3.點對點共享白板、PPT、投票功能失敗。 解決方法:1.檢查前端錯誤日誌、警告

【藍橋杯】第七屆國賽C語言B組 2.湊平方數dfs+STL

ble 一個 所有 mes char next memset target article 把0~9這10個數字,分成多個組,每個組恰好是一個平方數,這是能夠辦到的。比如:0, 36, 5948721 再比如:10985247361, 25, 63907840, 4, 28

UVALive - 6436 —DFS+思維

long long 我們 space cli 最大 %d while 復雜 路徑 題意:n個點連成的生成樹(n個點,n-1條邊,點與點之間都連通),如果某個點在兩點之間的路徑上,那這個點的繁榮度就+1,問你在所有點中,最大繁榮度是多少?就比如上面的圖中的C

BZOJ1064 NOI2008假面舞會dfs

則無 如果 online lag += col using 數量 noi   將圖中的環的長度定義為正向邊數量-反向邊數量,那麽答案一定是所有環的環長的gcd。dfs一下就能找到圖中的一些環,並且圖中的所有環的環長都可以由這些環長加加減減得到(好像不太會證)。如果有環長為1

[ZJOI2007]時態同步dfs+貪心

ostream 同時 一個 != n) htm code zjoi2007 zjoi 小Q在電子工藝實習課上學習焊接電路板。一塊電路板由若幹個元件組成,我們不妨稱之為節點,並將其用數字1,2,3.進行標號。電路板的各個節點由若幹不相交的導線相連接,且對於電路板的任何兩個節點

P3379 【模板】最近公共祖先LCAdfs

log 輸出格式 sample namespace i++ 技術分享 規模 link wap P3379 【模板】最近公共祖先(LCA) 題目描述 如題,給定一棵有根多叉樹,請求出指定兩個點直接最近的公共祖先。 輸入輸出格式 輸入格式: 第一行包含三個正整數

hdu - 1072dfs剪枝

space sin else 題目 ios class pan using cstring 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 思路:深搜每一個節點,並且進行剪枝,記錄每一步上一次的s1,s2;如果之前走過

小白python學習——機器學習篇——k-近鄰演算法KNN演算法

一、演算法理解 一般給你一資料集,作為該題目的資料(一個矩陣,每一行是所有特徵),而且每一組資料都是分了類,然後給你一個數據,讓這個你預測這組資料屬於什麼類別。你需要對資料集進行處理,如:歸一化數值。處理後可以用matplotlib繪製出影象,一般選兩個特徵繪製x,y軸,然後核心是計算出預測點到

B-DiningEK演算法

B - Dining   Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer J