HDU 2612 find a way

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2612

Q: It takes the shortest time for Y and m to go to the same KFC (there may be multiple KFC instances)

My method is to search for the sum of the shortest time of two people from KFC. The only thing that hurts is the first few of TLE's. Yes.

/**     Danceonly*/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;#define MAX(a,b) (a > b ? a : b)#define MIN(a,b) (a < b ? a : b)const int maxn = 10000007;struct node{      int x;      int y;}kfc[100005];struct abc{      int x,y;      int step;}q[100005],e;int maze[205][205],vis[205][205];int dir[4][2] = {1,0,0,1,-1,0,0,-1};int N,M;int BFS(int sx,int sy,int A){      int tail,head,step,x,y;      head = tail = 0;      q[head].x = sx;      q[head].y = sy;      q[head].step = 0;      vis[sx][sy] = 1;      while (head <= tail)      {            x = q[head].x;            y = q[head].y;            step = q[head].step;            for (int i=0;i<4;i++)            {                  e.x = x + dir[i][0];                  e.y = y + dir[i][1];                  if (maze[e.x][e.y] ==  0 || vis[e.x][e.y])continue ;                  e.step = step + 1;                  vis[e.x][e.y] = 1;                  if (maze[e.x][e.y] ==  A)                  {                        return e.step;                  }                  q[++tail] = e;            }            head++;      }      return maxn;}void show(){      for (int i=1;i<=N;i++)      {            for (int j=1;j<=M;j++)                  printf("%d ",maze[i][j]);            printf("\n");      }}void init(){      memset(vis,0,sizeof(vis));      memset(maze,0,sizeof(maze));      return ;}int main(){      while (scanf("%d%d",&N,&M) == 2)      {            int Yx,Yy,Mx,My;            memset(maze,0,sizeof(maze));            int c = 0;            for (int i=1;i<=N;i++)            {                  char ss[1005];                  scanf("%s",ss);                  for (int j=0;j<M;j++)                        if (ss[j] == '.')maze[i][j+1] = 1;                        else if (ss[j] == '@'){kfc[c].x = i;kfc[c++].y = j + 1;maze[i][j+1] = 1;}                        else if (ss[j] == 'Y'){maze[i][j+1] = 2;Yx = i;Yy = j + 1;}                        else if (ss[j] == 'M'){maze[i][j+1] = 3;Mx = i;My = j + 1;}            }            //show();            int ans = maxn;            for (int i=0;i<c;i++)            {                  int sum = 0;                  memset(vis,0,sizeof(vis));                  sum += BFS(kfc[i].x,kfc[i].y,2);                  if (sum + abs(kfc[i].x - Mx) + abs(kfc[i].y-My) >= ans)                        continue ;                  memset(vis,0,sizeof(vis));                  sum += BFS(kfc[i].x,kfc[i].y,3);                  if (ans > sum)ans = sum;            }            printf("%d\n",ans * 11);      }      return 0;}/*4 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.