Grid [Gym-100819O] [Problem D]

 1 #include <iostream>
 2 #include <cstdio>
 3 #include<queue>
 4 #include<cstring>
 5 #include <algorithm>
 6 using namespace std;
 7 int G[501][501];
 8 bool vis[501][501];
 9 int dx[4] = {-1,1,0,0};
10 int dy[4] = {0,0,1,-1};
11 typedef struct
12 {
13     int x,y,time;
14 }node;
15 void bfs(int m,int n)
16 {
17     node s;
18     s.x = 0;
19     s.y = 0;
20     s.time = 0;
21     queue<node> q;
22     q.push(s);
23     memset(vis,false,sizeof(vis));
24     vis[0][0] = true;
25     while(!q.empty())
26     {
27         node now = q.front(); q.pop();
28         //printf("%d %d\n",now.x,now.y);
29         if(now.x == m-1 && now.y==n-1) {
30             printf("%d",now.time);
31             return;
32         }
33         for(int i=0;i<4;i++)
34         {
35             int nx = now.x + dx[i]*G[now.x][now.y];
36             int ny = now.y + dy[i]*G[now.x][now.y];
37             //printf("-%d %d\n",nx,ny);
38             if(nx<0||nx>=m||ny<0||ny>=n||vis[nx][ny]) continue;
39             node nn;
40             nn.x = nx; nn.y = ny; nn.time = now.time+1;
41             q.push(nn);
42             vis[nx][ny] = true;
43         }
44     }
45     printf("IMPOSSIBLE");
46 }
47
48 int main()
49 {
50     int m,n;cin>>m>>n;
51     for(int i=0;i<m;i++)
52     {
53         string s;
54         cin>>s;
55         int len = s.length();
56         for(int j=0;j<len;j++)
57         {
58             G[i][j] = s[j]-‘0‘;
59         }
60     }
61     //puts("here");
62     bfs(m,n);
63 }

时间: 2024-10-26 18:39:21

Grid [Gym-100819O] [Problem D]的相关文章

Codeforces Gym 100342C Problem C. Painting Cottages 转化题意

Problem C. Painting CottagesTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/attachments Description The new cottage settlement is organized near the capital of Flatland. The construction company that is building the settl

Codeforces Gym 100610 Problem A. Alien Communication Masterclass 构造

Problem A. Alien Communication Masterclass Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100610 Description Andrea is a famous science fiction writer, who runs masterclasses for her beloved readers. The most popular one is the

Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100610 Description Robots are becoming more and more popular. They are used nowadays not only in manufacturing plants, but also at home. One programmer wit

Codeforces Gym 100610 Problem H. Horrible Truth 瞎搞

Problem H. Horrible Truth Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100610 Description In a Famous TV Show “Find Out” there are n characters and only one Horrible Truth. To make the series breathtaking all way long, the sc

Codeforces Gym 100610 Problem E. Explicit Formula 水题

Problem E. Explicit Formula Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100610 Description Consider 10 Boolean variables x1, x2, x3, x4, x5, x6, x7, x8, x9, and x10. Consider all pairs and triplets of distinct variables amon

Codeforces Gym 100342C Problem C. Painting Cottages 暴力

Problem C. Painting CottagesTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/attachments Description The new cottage settlement is organized near the capital of Flatland. The construction company that is building the sett

Codeforces Gym 100342D Problem D. Dinner Problem Dp+高精度

Problem D. Dinner ProblemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/attachments Description A group of k students from Cooking University living in the campus decided that each day of the semester one of them will p

Codeforces Gym 100500F Problem F. Door Lock 二分

Problem F. Door LockTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/attachments Description It was a beautiful night at Yekaterinburg, the sky was crystal clear with no clouds, and the view of the moon and the stars was

Codeforces Gym 100342H Problem H. Hard Test 构造题,卡迪杰斯特拉

Problem H. Hard TestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/attachments Description Andrew is having a hard time preparing his 239-th contest for Petrozavodsk. This time the solution to the problem is based on Di

Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset

Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/attachments Description The travel agency “Four Russians” is offering the new service for their clients. Unlike other agencies that only suggest one-way