zoukankan      html  css  js  c++  java
  • #292 (div.2) D.Drazil and Tiles (贪心+bfs)

    Description

    Drazil created a following problem about putting 1 × 2 tiles into an n × m grid:
    
    "There is a grid with some cells that are empty and some cells that are occupied. You should use 1 × 2 tiles to cover all empty cells and no two tiles should cover each other. And you should print a solution about how to do it."
    
    But Drazil doesn't like to write special checking program for this task. His friend, Varda advised him: "how about asking contestant only to print the solution when it exists and it is unique? Otherwise contestant may print 'Not unique' ".
    
    Drazil found that the constraints for this task may be much larger than for the original task!
    
    Can you solve this new problem?
    
    Note that you should print 'Not unique' either when there exists no solution or when there exists several different solutions for the original task.

    Input

    The first line contains two integers n and m (1 ≤ n, m ≤ 2000).
    
    The following n lines describe the grid rows. Character '.' denotes an empty cell, and the character '*' denotes a cell that is occupied.

    Output

    If there is no solution or the solution is not unique, you should print the string "Not unique".
    
    Otherwise you should print how to cover all empty cells with 1 × 2 tiles. Use characters "<>" to denote horizontal tiles and characters "^v" to denote vertical tiles. Refer to the sample test for the output format example.

    Sample Input

    Input
    3 3
    ...
    .*.
    ...
    Output
    Not unique
    Input
    4 4
    ..**
    *...
    *.**
    ....
    Output
    <>**
    *^<>
    *v**
    <><>
    Input
    2 4
    *..*
    ....
    Output
    *<>*
    <><>
    Input
    1 1
    .
    Output
    Not unique
    Input
    1 1
    *
    Output
    *

    Hint

    In the first case, there are indeed two solutions:
    
    
    <>^
    ^*v
    v<>
    and
    
    
    ^<>
    v*^
    <>v
    so the answer is "Not unique".

    Source

     
    用贪心法解决:首先应该着力填充周围只有一个空格的点,随后以它的空格为中心,再不断向外拓展,直到整个界面被填充。
    注意在judge函数判断边界和‘.’时应该这样写:return (i>=0 && i<n && j>=0 && j<m && mp[i][j]=='.');  分开判断一直错。
      1 #pragma comment(linker, "/STACK:1024000000,1024000000")
      2 #include<iostream>
      3 #include<cstdio>
      4 #include<cstring>
      5 #include<cmath>
      6 #include<math.h>
      7 #include<algorithm>
      8 #include<queue>
      9 #include<set>
     10 #include<bitset>
     11 #include<map>
     12 #include<vector>
     13 #include<stdlib.h>
     14 #include <stack>
     15 using namespace std;
     16 int dirx[]={0,0,-1,1};
     17 int diry[]={-1,1,0,0};
     18 #define PI acos(-1.0)
     19 #define max(a,b) (a) > (b) ? (a) : (b)  
     20 #define min(a,b) (a) < (b) ? (a) : (b)
     21 #define ll long long
     22 #define eps 1e-10
     23 #define MOD 1000000007
     24 #define N 2006
     25 #define inf 1e12
     26 int n,m;
     27 char mp[N][N];
     28 struct Node{
     29     int x,y;
     30 };
     31 char change[]="><<>v^^v";
     32 bool judge(int i,int j){
     33     return (i>=0 && i<n && j>=0 && j<m && mp[i][j]=='.');
     34 }
     35 
     36 int nearPoint_num(int x,int y){
     37     int ans=0;//ans表示周围的空点 
     38     for(int i=0;i<4;i++){
     39         int tx=x+dirx[i];
     40         int ty=y+diry[i];
     41         if(judge(tx,ty)){
     42             ans++;
     43         }
     44     }
     45     return ans;
     46 }
     47 
     48 
     49 void bfs(){
     50     queue<Node>q;
     51     Node tmp;
     52     Node t1,t2,t3;
     53     for(int i=0;i<n;i++){
     54         for(int j=0;j<m;j++){
     55             if(judge(i,j) && nearPoint_num(i,j)==1){
     56                 tmp.x=i;
     57                 tmp.y=j;
     58                 //printf("***%d %d
    ",tmp.x,tmp.y);
     59                 q.push(tmp);
     60             }
     61         }
     62     }
     63     while(!q.empty()){
     64         t1=q.front();
     65         q.pop();
     66         for(int i=0;i<4;i++){
     67             t2.x=t1.x+dirx[i];
     68             t2.y=t1.y+diry[i];
     69             if(judge(t2.x,t2.y)){
     70                 mp[t1.x][t1.y]=change[i*2];
     71                 mp[t2.x][t2.y]=change[i*2+1];
     72                 //printf("%d %d %c
    ",t1.x,t1.y,mp[t1.x][t1.y]);
     73                 //printf("%d %d %c
    ",t2.x,t2.y,mp[t2.x][t2.y]);
     74                 
     75                 for(int j=0;j<4;j++){
     76                     t3.x=t2.x+dirx[j];
     77                     t3.y=t2.y+diry[j];
     78                     if(judge(t3.x,t3.y) && nearPoint_num(t3.x,t3.y)==1){
     79                         q.push(t3);
     80                     }
     81                 }
     82                 
     83             }
     84             
     85         }
     86     }
     87     
     88     int flag=1;
     89     for(int i=0;i<n;i++){
     90         for(int j=0;j<m;j++){
     91             if(mp[i][j]=='.'){
     92                 flag=0;
     93                 break;
     94             }
     95         }
     96         if(flag==0){
     97             break;
     98         }
     99     }
    100     if(flag==0){
    101         printf("Not unique
    ");
    102     }
    103     else{
    104         for(int i=0;i<n;i++){
    105             for(int j=0;j<m;j++){
    106                 printf("%c",mp[i][j]);
    107             }
    108             printf("
    ");
    109         }
    110     }
    111     
    112 }
    113 int main()
    114 {
    115     while(scanf("%d%d",&n,&m)==2){
    116         for(int i=0;i<n;i++){
    117             scanf("%s",mp[i]);
    118         }
    119         
    120         bfs();
    121         
    122     }
    123     return 0;
    124 } 
    View Code
  • 相关阅读:
    睡前一分钟打造完美下半身 健康程序员,至尚生活!
    几种不伤身体的速效减肥秘方 健康程序员,至尚生活!
    头发一周洗几次才适宜? 健康程序员,至尚生活!
    夏日驱蚊虫蟑螂的最好办法! 健康程序员,至尚生活!
    WPF控件和布局
    《深入浅出WPF》笔记——绑定篇(二)
    WPF中的DataTemplate绑定使用的场合
    WPF第一个程序和XAML初探
    实习总结之jquery实例
    下一步要实战的东西
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4839330.html
Copyright © 2011-2022 走看看