• hdu 1175 连连看


    连连看

    Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 19491    Accepted Submission(s): 5054


    Problem Description
    “连连看”相信很多人都玩过。没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子。如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子),而且线的转折次数不超过两次,那么这两个棋子就可以在棋盘上消去。不好意思,由于我以前没有玩过连连看,咨询了同学的意见,连线不能从外面绕过去的,但事实上这是错的。现在已经酿成大祸,就只能将错就错了,连线不能从外围绕过。
    玩家鼠标先后点击两块棋子,试图将他们消去,然后游戏的后台判断这两个方格能不能消去。现在你的任务就是写这个后台程序。
     
    Input
    输入数据有多组。每组数据的第一行有两个正整数n,m(0<n<=1000,0<m<1000),分别表示棋盘的行数与列数。在接下来的n行中,每行有m个非负整数描述棋盘的方格分布。0表示这个位置没有棋子,正整数表示棋子的类型。接下来的一行是一个正整数q(0<q<50),表示下面有q次询问。在接下来的q行里,每行有四个正整数x1,y1,x2,y2,表示询问第x1行y1列的棋子与第x2行y2列的棋子能不能消去。n=0,m=0时,输入结束。
    注意:询问之间无先后关系,都是针对当前状态的!
     
    Output
    每一组输入数据对应一行输出。如果能消去则输出"YES",不能则输出"NO"。
     
    Sample Input
    3 4
    1 2 3 4
    0 0 0 0
    4 3 2 1
    4
    1 1 3 4
    1 1 2 4
    1 1 3 3
    2 1 2 4
    3 4
    0 1 4 3
    0 2 4 1
    0 0 0 0
    2
    1 1 2 4
    1 3 2 3
    0 0
     
     
    Sample Output
    YES NO NO NO NO YES
     
    Author
    lwg
     
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cmath>
      4 #include<cstdlib>
      5 #include<cstring>
      6 #include<queue>
      7 using namespace std;
      8 int map[1005][1005];
      9 int vis[1005][1005];
     10 int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
     11 int n,m;
     12 int x1i,y1j,x2i,y2j;
     13 int flag;
     14 struct ln{
     15     int x;
     16     int y;
     17     int step;
     18 }abc,q;
     19 
     20 void bfs()
     21 {
     22     int i;
     23     queue<struct ln>p;
     24     memset(vis,0,sizeof(vis));
     25     abc.x=x1i;
     26     abc.y=y1j;
     27     abc.step=-1;
     28     vis[abc.x][abc.y]=1;
     29     p.push(abc);
     30     while(!p.empty())
     31     {
     32         abc=p.front();
     33         p.pop();
     34 //        if( abc.x==x2i && abc.y==y2j )//判断条件
     35 //        {
     36 //            if(abc.step<=2)//放里面
     37 //            flag=1;
     38 //            return ;
     39 //        }
     40         abc.step++;//每次取值都代表上一个值的四周都已遍历所以++代表一次转弯
     41 
     42         for(i=0;i<4;i++)
     43         {
     44             q.x=abc.x+dir[i][0];
     45             q.y=abc.y+dir[i][1];
     46             q.step=abc.step;
     47             while(q.x>=1&&q.x<=n &&q.y>=1&&q.y<=m &&!vis[q.x][q.y])
     48             {
     49                 if(map[q.x][q.y]!=0)//当map不为0时
     50                 {
     51                     if(q.x!=x2i || q.y!=y2j)//当不是终点时,标记走过
     52                     {
     53                         vis[q.x][q.y]=1;
     54                         break;
     55                     }
     56                     if(q.x==x2i && q.y==y2j &&q.step<=2)//当时终点时,判断转弯数
     57                     {
     58                         flag=1;
     59                         return ;
     60                     }
     61                 }
     62                 vis[q.x][q.y]=1;//其他为0的情况
     63                 p.push(q);
     64                 q.x+=dir[i][0];
     65                 q.y+=dir[i][1];
     66             }
     67         }
     68     }
     69     return ;
     70 }
     71 
     72 int main()
     73 {
     74     //freopen("in.txt","r",stdin);
     75     while(~scanf("%d%d",&n,&m))
     76     {
     77         if(!n&&!m)
     78         break;
     79         int i,j;
     80         for(i=1;i<=n;i++)
     81         {
     82             for(j=1;j<=m;j++)
     83             {
     84                 scanf("%d",&map[i][j]);
     85             }
     86         }
     87         int q;
     88         scanf("%d",&q);
     89         for(i=1;i<=q;i++)
     90         {
     91             flag=0;
     92             scanf("%d%d%d%d",&x1i,&y1j,&x2i,&y2j);
     93             if(map[x1i][y1j]!=map[x2i][y2j] || map[x1i][y1j]==0 || map[x2i][y2j]==0)
     94             {
     95                 printf("NO
    ");
     96                 continue;
     97             }
     98 
     99             bfs();
    100 
    101             if(flag==1)
    102             printf("YES
    ");
    103             else
    104             printf("NO
    ");
    105 
    106         }
    107 
    108 
    109     }
    110     return 0;
    111 }
    View Code
     
  • 相关阅读:
    博客园页面设置(转载)
    正则表达式30分钟入门教程 (转载)
    如何写出优雅的代码
    centos7 nginx+php5.6+mysql安装与配置
    git 进阶
    js 异步解决方案
    行动派
    unicode 与 utf-8
    bower command not found--windows
    click事件细节
  • 原文地址:https://www.cnblogs.com/xuesen1995/p/4120018.html
Copyright © 2020-2023  润新知