Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 400400 points
Problem Statement
There is a H \times WH×W-square grid with HH horizontal rows and WW vertical columns. Let (i, j)(i,j) denote the square at the ii-th row from the top and jj-th column from the left.
Each square is described by a character C_{i, j}Ci,j, where C_{i, j} =Ci,j= .
means (i, j)(i,j) is an empty square, and C_{i, j} =Ci,j= #
means (i, j)(i,j) is a wall.
Takahashi is about to start walking in this grid. When he is on (i, j)(i,j), he can go to (i, j + 1)(i,j+1) or (i + 1, j)(i+1,j). However, he cannot exit the grid or enter a wall square. He will stop when there is no more square to go to.
When starting on (1, 1)(1,1), at most how many squares can Takahashi visit before he stops?
Constraints
- 1 \leq H, W \leq 1001≤H,W≤100
- HH and WW are integers.
- C_{i, j} =Ci,j=
.
or C_{i, j} =Ci,j=#
. (1 \leq i \leq H, 1 \leq j \leq W)(1≤i≤H,1≤j≤W) - C_{1, 1} =C1,1=
.
Input
Input is given from Standard Input in the following format:
HH WW C_{1, 1} \ldots C_{1, W}C1,1…C1,W \vdots⋮ C_{H, 1} \ldots C_{H, W}CH,1…CH,W
Output
Print the answer.
Sample Input 1 Copy
Copy
3 4 .#.. ..#. ..##
Sample Output 1 Copy
Copy
4
For example, by going (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (3, 2)(1,1)→(2,1)→(2,2)→(3,2), he can visit 44 squares.
He cannot visit 55 or more squares, so we should print 44.
Sample Input 2 Copy
Copy
1 1 .
Sample Output 2 Copy
Copy
1
Sample Input 3 Copy
Copy
5 5 ..... ..... ..... ..... .....
Sample Output 3 Copy
Copy
9
意思:每次从(1,1)出发,方向只可以向下或向右,‘#’为墙不可走求最多可以走多少步。
知识点:搜索
#include<iostream>
#include<stdio.h>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<deque>
#include<vector>
#include<queue>
#include<string>
#include<cstring>
#include<map>
#include<stack>
#include<set>
using namespace std;
const int N=505;
int n,m;
char g[N][N];
int dx[2]={0,1},dy[2]={1,0};
int ans=0;
void dfs(int x,int y,int f)
{
ans=max(ans,f);
for(int i=0;i<2;i++)
{
int a=x+dx[i],b=y+dy[i];
if(a>=1&&a<=n&&b>=1&&b<=m&&g[a][b]=='.')
{
g[a][b]='#';
dfs(a,b,f+1);
}
}
ans=max(ans,f);
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>g[i][j];
}
}
dfs(1,1,1);
cout<<ans<<endl;
return 0;
}
void要有int替换时一定要有返回值。