-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathabcpath.cpp
68 lines (67 loc) · 2.15 KB
/
abcpath.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include"iostream"
#include"cmath"
#include"vector"
#include"algorithm"
using namespace std;
int length(vector <string> grid);
int path(vector<vector<int> >&marked,vector<string>grid,int i,int j,int len,int len1,int asc);
int main()
{
int h,w;cin>>h>>w;int cas=1;
while(!(h==0&&w==0))
{
vector<string>grid(h);
for(int i=0;i<h;++i)
cin>>grid[i];
cout<<"Case "<<cas++<<": "<<length(grid)<<endl;
cin>>h>>w;
}
}
int length(vector <string> grid)
{
int len=grid.size(),len1=grid[0].length();
vector<int>temp(len1,-1);
vector<vector<int> >marked(len,temp);
int m1=0;
for(int i=0;i<len;i++)
{
for(int j=0;j<len1;j++)
{
char ch=grid[i][j];
if(ch=='A')
{
int path1=path(marked,grid,i,j,len,len1,65);
m1=max(m1,path1);
}
}
}
return m1;
}
int path(vector<vector<int> >&marked,vector<string>grid,int i,int j,int len,int len1,int asc)
{
if(!((i>=0&&i<len)&&(j>=0&&j<len1)))
return 0;
char ch=grid[i][j];
if(asc==90)
{
if(ch=='Z')
return 1;
else
return 0;
}
int as=ch;
if(as!=asc)
return 0;
if(marked[i][j]!=-1)
return marked[i][j];
marked[i][j]=path(marked,grid,i+1,j,len,len1,asc+1);
marked[i][j]=max(marked[i][j],path(marked,grid,i-1,j,len,len1,asc+1));
marked[i][j]=max(marked[i][j],path(marked,grid,i,j-1,len,len1,asc+1));
marked[i][j]=max(marked[i][j],path(marked,grid,i,j+1,len,len1,asc+1));
marked[i][j]=max(marked[i][j],path(marked,grid,i+1,j+1,len,len1,asc+1));
marked[i][j]=max(marked[i][j],path(marked,grid,i+1,j-1,len,len1,asc+1));
marked[i][j]=max(marked[i][j],path(marked,grid,i-1,j+1,len,len1,asc+1));
marked[i][j]=max(marked[i][j],path(marked,grid,i-1,j-1,len,len1,asc+1));
marked[i][j]++;
return marked[i][j];
}