Solution to a problem - the line of fire

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

Solution to a problem - the line of fire

Description
going to give you N N map, there is a map on the wall, with an "X", free to use. "" Representation. Can put soldiers on the open space, the soldiers can attack each four straight about his range and down (not through walls)
asked each other to prevent accidental injury premise of (any one of the soldiers is not within range of the other soldiers), the number of soldiers placed the most.
Input
The first line gives a number n
of the following lines are n N
N map (0 <= n <=. 8)
the Output
corresponding to each set of data to put the maximum number of soldiers, each set of data per line.
The Input the Sample
4
.X ...
...
XX ...
...
the Sample the Output
5

#include<bits/stdc++.h>
using namespace std;
int ans,lon;
char ch[15][15];//cmp
 
bool check(int x,int y)
{
    for (int i=y;i<=lon;i++)
    {
        if (ch[x][i]=='X')
            break;
        if (ch[x][i]=='D')
            return false;
    }
    for (int i=y;i>=1;i--)
    {
        if (ch[x][i]=='X')
            break;
        if (ch[x][i]=='D')
            return false;
    }
    for (int i=x;i>=1;i--)
    {
        if (ch[i][y]=='X')
            break;
        if (ch[i][y]=='D')
            return false;
    }
    for (int i=x;i<=lon;i++)
    {
        if (ch[i][y]=='X')
            break;
        if (ch[i][y]=='D')
            return false;
    }
    return true;
}
void find(int num,int x,int y)
{
    if (num>ans)
            ans=num;
    for (int i=x;i<=lon;i++)
        for (int j=1;j<=lon;j++)
            if (check(i,j) && ch[i][j]=='.')//扩展方式所达到状态合法 
            {  
                ch[i][j]='D';//根据题意来添加  
            //标记;  
                find(num+1,i,j);// dfs();  
                ch[i][j]='.';  
            //是否还原标记根据题意  
            //如果加上(还原标记)就是 回溯法  
            }  
}
 
int main()
{
    cin>>lon;
    for (int i=1;i<=lon;i++)
        for (int j=1;j<=lon;j++)
            cin>>ch[i][j];
     
    find(0,1,1);
    cout<<ans<<endl;
    return 0;   
}

Guess you like

Origin blog.csdn.net/Tangwan_jeff/article/details/95200548