hdu 1175 连连看

来源:互联网 发布:广州能源所怎么样 知乎 编辑:程序博客网 时间:2024/06/05 04:47

市赛下铁很不开心,以后只专心程序,以后再也不会和某个人组队了,比个赛只会说 you can you up ,no can no bb的沙雕,我能怎么办,比自己强的各种佩服,和自己差不多,和比自己弱的一脸不屑,其实你打的代码是个卵子,全程ac代码都是我和另一个队友打得;
看人要深看
连连看
Time Limit: 10000msMemory Limit: 32768KB This problem will be judged on HDU. Original ID: 1175
64-bit integer IO format: %I64d Java class name: Main
Prev Submit Status Statistics Next
Type:
None

Tag it!
“连连看”相信很多人都玩过。没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子。如果某两个相同的棋子,可以通过一条线连起来(这条线不能经过其它棋子),而且线的转折次数不超过两次,那么这两个棋子就可以在棋盘上消去。不好意思,由于我以前没有玩过连连看,咨询了同学的意见,连线不能从外面绕过去的,但事实上这是错的。现在已经酿成大祸,就只能将错就错了,连线不能从外围绕过。
玩家鼠标先后点击两块棋子,试图将他们消去,然后游戏的后台判断这两个方格能不能消去。现在你的任务就是写这个后台程序。
Input
输入数据有多组。每组数据的第一行有两个正整数n,m(0

#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;int map[1001][1001], hust[1001][1001];int n, m;struct node{    int x, y, d, turn;};int dir[4][2] = { 1,0,-1,0,0,-1,0,1 };queue<node> q;int bfs(int x1, int y1, int x2, int y2){    node t, p;    while (!q.empty())    {        p = q.front();        q.pop();        if(p.x==x2&&p.y==y2&&p.turn<=2)            return 1;        for (int i = 0; i < 4; i++)        {            t.x = p.x + dir[i][0];            t.y = p.y + dir[i][1];            if (t.x<1 || t.y<1 || t.x>n || t.y>m)                continue;            if (p.d == i)            {                t.d = p.d;                t.turn = p.turn;            }            else            {                t.turn = p.turn + 1;                t.d = i;            }            if (t.turn > 2)                continue;            if ((!map[t.x][t.y] || (t.x == x2&&t.y == y2)) && t.turn <= hust[t.x][t.y])            {                q.push(t);                hust[t.x][t.y] = t.turn;            }        }    }    return 0;}int main(){    //int n, m;    while (~scanf("%d %d",&n,&m) &&(n+m))    {        int i, j;        for (i = 1; i <= n; i++)            for (j = 1; j <= m; j++)                scanf("%d", &map[i][j]);        int k;        scanf("%d", &k);        int x1, x2, y1, y2;        while (k--)        {            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);            if (map[x1][y1] != map[x2][y2] || !map[x1][y1] || !map[x2][y2] || (x1 == x2&&y1 == y2))                puts("NO");            else            {                while (!q.empty())                    q.pop();                for (i = 1; i <= n; i++)                    for (j = 1; j <= m; j++)                        hust[i][j] = 11;                hust[x1][y1] = 0;                node t;                for (i = 0; i < 4; i++)                {                    t.x = x1;                    t.y = y1;                    t.turn = 0;                    t.d = i;                    q.push(t);                }                if (bfs(x1, y1, x2, y2))                    puts("YES");                else                    puts("NO");            }        }    }    return 0;   }Powered by NIT ACM Team | Current Style: Cerulean.Select Style:     Fluid Width?

这个看不懂看这个,理解这个,就理解上一个了

#include<cstdio>  #include<cstring>  #include<algorithm>  #include<queue>  #define MAXN 1005  using namespace std;  int map[MAXN][MAXN],v[MAXN][MAXN];  const int dx[4] = {0,1,0,-1};  const int dy[4] = {-1,0,1,0};  int n,m,flag;  struct node  {      int x,y;      int dir;//记录方向      int step;//记录转弯次数  };  int bfs(int x1,int y1,int x2,int y2)  {      memset(v,1,sizeof(v));      queue <node> q;      node s,temp;      s.x = x1;      s.y = y1;      s.dir = -1;//-1表示朝哪个方向都可以      s.step = 0;      q.push(s);      while(!q.empty())      {          temp = q.front();          q.pop();          if(temp.x == x2 && temp.y == y2 && temp.step <= 2)              return 1;          for(int i = 0; i < 4; i ++)          {              s = temp;              s.x += dx[i];              s.y += dy[i];              if(s.x >= 1 && s.x <= n && s.y >= 1 && s.y <= m && (map[s.x][s.y] == 0 || (s.x == x2 && s.y == y2)))              {                  if(s.dir != -1)                  {                      if(s.dir != i)                      {                          s.dir = i;                          s.step ++;                      }                  }                  else s.dir = i;                  if(s.step > 2) continue;                  if(s.step <= v[s.x][s.y])//保证转弯次数最少,等号不能忘记                  {                      v[s.x][s.y] = s.step;                      q.push(s);                  }              }          }      }      return 0;  }  int main()  {      int i,j,t,x1,y1,x2,y2;      while(scanf("%d%d",&n,&m),(n+m))      {          for(i = 1; i <= n; i ++)          for(j = 1; j <= m; j ++)          scanf("%d",&map[i][j]);          scanf("%d",&t);          while(t--)          {              scanf("%d%d%d%d",&x1,&y1,&x2,&y2);              if(!map[x1][y1] || !map[x2][y2] || map[x1][y1] != map[x2][y2] || (x1 == x2 && y1 == y2) )                  flag = 0;              else flag = bfs(x1,y1,x2,y2);              if(flag) puts("YES");              else puts("NO");          }      }      return 0;  }  
0 0
原创粉丝点击