Labyrinth(記憶化BFS)
Labyrinth
http://codeforces.com/problemset/problem/1064/D
time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard outputYou are playing some computer game. One of its levels puts you in a maze consisting of n
Unfortunately, your keyboard is about to break, so you can move left no more than x times and move right no more than y times. There are no restrictions on the number of moves up and down since the keys used to move up and down are in perfect condition.
Now you would like to determine for each cell whether there exists a sequence of moves that will put you from the starting cell to this particular one. How many cells of the board have this property?
The first line contains two integers n, m (1 ≤ n, m ≤ 2000) — the number of rows and the number columns in the labyrinth respectively.
The second line contains two integers r, c (1 ≤ r ≤ n, 1 ≤ c ≤ m) — index of the row and index of the column that define the starting cell.
The third line contains two integers x, y (0 ≤ x, y ≤ 109) — the maximum allowed number of movements to the left and to the right respectively.
The next n lines describe the labyrinth. Each of them has length of m and consists only of symbols '.' and '*'. The j-th character of the i-th line corresponds to the cell of labyrinth at row i and column j. Symbol '.' denotes the free cell, while symbol '*' denotes the cell with an obstacle.
It is guaranteed, that the starting cell contains no obstacles.
OutputPrint exactly one integer — the number of cells in the labyrinth, which are reachable from starting cell, including the starting cell itself.
Examples input Copy4 5output Copy
3 2
1 2
.....
.***.
...**
*....
10input Copy
4 4output Copy
2 2
0 1
....
..*.
....
....
7Note
Cells, reachable in the corresponding example, are marked with '+'.
First example:
+++..
+***.
+++**
*+++.
Second example:
.++.
.+*.
.++.
.++.
加個記憶化,判斷L和R剩餘多少就行
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<string> 6 #include<algorithm> 7 #include<queue> 8 #include<vector> 9 #pragma GCC optimize(2) 10 using namespace std; 11 12 int n,m; 13 char map[2005][2005]; 14 struct Num{ 15 int L,R; 16 }book[2005][2005]; 17 int r,c,L,R; 18 struct sair{ 19 int x,y,L,R; 20 }; 21 int dir[4][2]={0,-1,-1,0,0,1,1,0};//R,D,L,U 22 23 void bfs(){ 24 queue<sair>Q; 25 sair s,e; 26 s.x=r,s.y=c,s.L=L,s.R=R; 27 Q.push(s); 28 book[s.x][s.y].L=L; 29 book[s.x][s.y].R=R; 30 while(!Q.empty()){ 31 s=Q.front(); 32 Q.pop(); 33 for(int i=0;i<4;i++){ 34 e.x=s.x+dir[i][0]; 35 e.y=s.y+dir[i][1]; 36 if(e.x>=0&&e.x<n&&e.y>=0&&e.y<m&&map[e.x][e.y]!='*'){ 37 e.L=s.L; 38 e.R=s.R; 39 if(i==2){ 40 e.R--; 41 if(e.R<0) continue; 42 } 43 else if(i==0){ 44 e.L--; 45 if(e.L<0) continue; 46 } 47 if(book[e.x][e.y].L<e.L||book[e.x][e.y].R<e.R){ 48 book[e.x][e.y].L=max(book[e.x][e.y].L,e.L); 49 book[e.x][e.y].R=max(book[e.x][e.y].R,e.R); 50 Q.push(e); 51 } 52 53 } 54 } 55 } 56 } 57 58 int main(){ 59 cin>>n>>m; 60 cin>>r>>c; 61 cin>>L>>R; 62 for(int i=0;i<=2000;i++){ 63 for(int j=0;j<=2000;j++){ 64 book[i][j].L=book[i][j].R=-1; 65 } 66 } 67 for(int i=0;i<n;i++){ 68 cin>>map[i]; 69 } 70 r--,c--; 71 bfs(); 72 int ans=0; 73 for(int i=0;i<n;i++){ 74 for(int j=0;j<m;j++){ 75 if(book[i][j].L!=-1||book[i][j].R!=-1){ 76 ans++; 77 } 78 } 79 } 80 cout<<ans<<endl; 81 }View Code