Battleship |
Time Limit: 2000ms, Special Time Limit:5000ms, Memory Limit:262144KB |
Total submit users: 16, Accepted users: 11 |
Problem 12906 : Special judge |
Problem description |
You are in a computer security business. Recently one of your clients developed a new test designed to separate humans from programs. Your task is to test its efficiency. |
Input |
The input file consists of ten lines, containing ten numbers each. Each number represents the round at which the corresponding cell will be shot. All numbers are distinct and belong to the range 1...100. |
Output |
Output the optimal ship arrangement for the battleship test. Empty cells should be represented by the dot (‘.’), occupied cells should by represented by the number sign (‘#’). |
Sample Input |
1 2 3 4 5 6 7 8 9 10 36 37 38 39 40 41 42 43 44 11 35 64 65 66 67 68 69 70 45 12 34 63 84 85 86 87 88 71 46 13 33 62 83 96 97 98 89 72 47 14 32 61 82 95 100 99 90 73 48 15 31 60 81 94 93 92 91 74 49 16 30 59 80 79 78 77 76 75 50 17 29 58 57 56 55 54 53 52 51 18 28 27 26 25 24 23 22 21 20 19 |
Sample Output |
...####... .......... #....##... #.#.....#. ........#. ...###.... .#........ ........#. ..#.....#. .....#..#. |
题意:按照1-100的顺序打。有1艘4个格子的船,2艘3个格子的船,3艘2个格子的船,4艘1个格子的船。船每个格子都被打到一次就沉默了。问要使船最后沉没怎么安排船的位置。用#表示船。#不能相邻也不能有边角。就是右下也不行。
@darkdream 用枚举构造Accept.赞!
#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>
#define LL long long
using namespace std;
int mp[20][20];
int check(int i,int j)
{
int sum = 0 ;
if(i <= 10 && i >= 1 && j <= 10 && j >=1 )
{
sum += mp[i+1][j] + mp[i][j] + mp[i-1][j] +mp[i][j+1] + mp[i][j-1] + mp[i+1][j+1] + mp[i+1][j-1] + mp[i-1][j+1] + mp[i-1][j-1] ;
if(sum == 0 )
return 1;
return 0 ;
}
return 0 ;
}
int ship[] = {0,4,3,3,2,2,2,1,1,1,1};
int hs[20];
int dfs(int x, int y , int k ,int dis)
{
if(k == 1 )
return 1;
if(dis == 1)
{
int tx = x + 1;
int ty = y;
if(check(tx,ty))
{
if(dfs(tx,ty,k-1,dis))
{
mp[tx][ty] =1 ;
return 1;
}
}else{
return 0 ;
}
}else if(dis == 2){
int tx = x -1;
int ty = y;
if(check(tx,ty))
{
if(dfs(tx,ty,k-1,dis))
{
mp[tx][ty] =1 ;
return 1;
}
}else{
return 0 ;
}
}else if(dis == 3){
int tx = x;
int ty = y+1;
if(check(tx,ty))
{
if(dfs(tx,ty,k-1,dis))
{
mp[tx][ty] =1 ;
return 1;
}
}else{
return 0 ;
}
}else {
int tx = x;
int ty = y-1;
if(check(tx,ty))
{
if(dfs(tx,ty,k-1,dis))
{
mp[tx][ty] =1 ;
return 1;
}
}else{
return 0 ;
}
}
return 0 ;
}
void solve(int x, int y )
{
//printf("%d %d\n",x,y);
for(int i = 1;i <= 9 ;i ++)
if(hs[i] == 0 )
for(int j = 1;j <= 4 ;j ++)
{
if(dfs(x,y,ship[i],j))
{
mp[x][y] = 1;
hs[i] = 1;
return ;
}
}
}
int main(){
int temp ;
while(scanf("%d",&temp) != EOF)
{
memset(mp,0,sizeof(mp));
memset(hs,0,sizeof(hs));
if(temp == 100 )
{
mp[1][1] = 1;
}
for(int i = 2;i <= 10;i ++)
{
scanf("%d",&temp);
if(temp == 100 )
{
mp[1][i] = 1;
}
}
for(int i = 2;i <= 10;i ++)
{
for(int j = 1;j <= 10; j ++)
{
scanf("%d",&temp);
if(temp == 100 )
{
mp[i][j] = 1;
}
}
}
for(int i =1;i <= 10;i ++)
{
for(int j = 1;j <= 10; j ++)
{
if(check(i,j))
{
solve(i,j);
}
}
}
for(int i =1;i <= 10 ;i ++)
{ for(int j = 1;j <= 10 ;j ++)
{
if(mp[i][j])
{
printf("#");
}else printf(".");
}
printf("\n");
}
}
return 0;
}