-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.cpp
More file actions
190 lines (182 loc) · 4.05 KB
/
Copy pathtemp.cpp
File metadata and controls
190 lines (182 loc) · 4.05 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Date: 13-12-2025
// Start Time: 18:14:02
// End Time : 18:36:52
// Time Taken: 22 minutes
// Author: Shashwat Kumar
// QUESTION LINK:
// Rating:
// Description:
// Solved:
// Learning:
/****************************************************Pre Processor***************************************************/
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
#ifdef LOCAL
#include "./lib/print.h"
#include "./lib/debug.h"
#else
#define debug(...) 42
#endif
#define endl '\n'
/***************************************************Main Function**************************************************/
/*
a - z - warp cell
. - empty cell
# - obstacle
dfs in 4 directions but with extra edges
*/
int n, m;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
vector<vector<pair<int, int>>> warp(26);
vector<vector<int>> vis;
bool check(int x, int y, vector<vector<char>> &v)
{
return x >= 0 && x < n && y >= 0 && y < m && v[x][y] != '#' && vis[x][y] == -1;
}
bool dfs(vector<vector<char>> &v, int row, int col)
{
if (row == n - 1 && col == m - 1)
return true;
debug(row, col);
vis[row][col] = 1;
bool ans = false;
for (int i = 0; i < 4; i++)
{
int nrow = row + dx[i];
int ncol = col + dy[i];
if (check(nrow, ncol, v))
{
ans |= dfs(v, nrow, ncol);
if (ans)
return true;
}
}
if (v[row][col] >= 'a' && v[row][col] <= 'z')
{
// warping
int idx = v[row][col] - 'a';
for (int i = 0; i < warp[idx].size(); i++)
{
pair<int, int> neigh = warp[idx][i];
if (vis[neigh.first][neigh.second] == -1)
{
ans |= dfs(v, neigh.first, neigh.second);
}
}
}
return ans;
}
void solve()
{
cin >> n >> m;
vector<vector<char>> v(n, vector<char>(m));
vis.assign(n, vector<int>(m, -1));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> v[i][j];
if (v[i][j] >= 'a' && v[i][j] <= 'z')
warp[v[i][j] - 'a'].push_back({i, j});
}
}
if (dfs(v, 0, 0))
cout << 1;
else
cout << -1;
}
signed main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(false);
cin.tie(NULL);
std::cout.tie(NULL);
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++)
{
#ifdef LOCAL
std::cerr << "Case # " << i << endl;
std::cout << "Case #" << i << endl;
#endif
solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef struct node
{
int x,y,step;
}NODE,*PNODE;
vector<pair<int,int> > vec[26];
queue<NODE> q;
char a[1005][1005];
int dx[]={0,-1,0,1,0};
int dy[]={0,0,1,0,-1};
int dist[1005][1005];
int n,m;
int flag[1000005];
void bfs()
{
dist[1][1]=0;
q.push(NODE{1,1,0});
int i,tx,ty;
NODE tmp;
while(!q.empty())
{
tmp=q.front();
q.pop();
for(i=1;i<=4;i++)
{
tx=tmp.x+dx[i];
ty=tmp.y+dy[i];
if(tx<1||tx>n||ty<1||ty>m||dist[tx][ty]!=-1||a[tx][ty]=='#')
{
continue;
}
dist[tx][ty]=dist[tmp.x][tmp.y]+1;
q.push(NODE{tx,ty,dist[tx][ty]});
}
if(a[tmp.x][tmp.y]>='a'&&a[tmp.x][tmp.y]<='z'&&flag[a[tmp.x][tmp.y]-'a']==0)
{
flag[a[tmp.x][tmp.y]-'a']=1;
for(i=0;i<vec[a[tmp.x][tmp.y]-'a'].size();i++)
{
tx=vec[a[tmp.x][tmp.y]-'a'][i].first;
ty=vec[a[tmp.x][tmp.y]-'a'][i].second;
if(dist[tx][ty]!=-1)
{
continue;
}
dist[tx][ty]=dist[tmp.x][tmp.y]+1;
q.push(NODE{tx,ty,dist[tx][ty]});
}
}
}
}
int main()
{
scanf("%d %d\n",&n,&m);
int i,j;
for(i=1;i<=n;i++)
{
scanf("%s",a[i]+1);
for(j=1;j<=m;j++)
{
if(a[i][j]>='a'&&a[i][j]<='z')
{
vec[a[i][j]-'a'].push_back(make_pair(i,j));
}
}
}
memset(dist,0xff,sizeof(dist));
bfs();
printf("%d",dist[n][m]);
return 0;
}