-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdream.cpp
More file actions
146 lines (133 loc) · 3.97 KB
/
dream.cpp
File metadata and controls
146 lines (133 loc) · 3.97 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
#include <bits/stdc++.h>
using namespace std;
//#include <ext/rope>
//using namespace __gnu_cxx;
#include <ext/pb_ds/assoc_container.hpp>
#include <stdlib.h>
using namespace __gnu_pbds;
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#define inputJunk ios_base::sync_with_stdio(0); cin.tie(0);
#define pb push_back
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define debug cout<<"HERE"<<endl;
#define ell "\n"
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<int, pi> trip;
typedef pair<pll, ll> tripll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
typedef vector<trip> vtrip;
typedef vector<tripll> vtripll;
const int INF = 1e9+1212;
const ll P = 9973, M = 1e9+9;
const int MM = 1e3+5; int mod = 1e9+7;//99824435
int n, m, grid[MM][MM];
int step[MM][MM][2], dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, -1, 1};
bool inbound(int x, int y){
return x>=0&&x<n&&y>=0&&y<m;
}
int calc(int a, int b){
if(a==-1){
return b;
}
if(b==-1){
return a;
}
return min(a, b);
}
int main(){
freopen("dream.in","r", stdin);
//freopen("dream.out","w", stdout);
inputJunk
cin>>n>>m;
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
step[i][j][0] = -1;step[i][j][1] = -1;
cin>>grid[i][j];
}
}
q.push({{{0, 0}, -1}, 0});
step[0][0][0] = 0;
while(!q.empty()){
int curx = q.front().f.f.f, cury = q.front().f.f.s; bool isorange = q.front().s;
int dir = q.front().f.s;
cout<<curx<<" "<<cury<<" "<<boolalpha<<" "<<isorange<<ell;
q.pop();
for(int i = 0;i<4;i++){
if(dir!=-1&&i!=dir)continue;
int newx = curx+dx[i], newy = cury+dy[i];
if(inbound(newx, newy)&&(step[newx][newy][isorange]==-1||grid[newx][newy]==4)){
bool neworange = isorange;
if(grid[newx][newy]==0)continue;
if(grid[newx][newy]==3&&!isorange)continue;
if(grid[newx][newy]==2)neworange = true;
if(grid[newx][newy]==4){
neworange = false;
int newnewx = newx+dx[i], newnewy = newy+dy[i];
if(!inbound(newnewx, newnewy))continue;
if(grid[newnewx][newnewy]==3||grid[newnewx][newnewy]==0){
step[newx][newy][neworange] = step[curx][cury][isorange]+1;
q.push({{{newx, newy}, -1}, neworange});
continue;
}
step[newx][newy][neworange] = step[curx][cury][isorange]+1;
q.push({{{newx, newy}, i}, neworange});
continue;
}
step[newx][newy][neworange] = step[curx][cury][isorange]+1;
q.push({{{newx, newy}, -1}, neworange});
}
}
}
/* for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
cout<<step[i][j][0]<<" ";
}cout<<ell;
}cout<<ell;
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
cout<<step[i][j][1]<<" ";
}cout<<ell;
}cout<<ell;*/
cout<<calc(step[n-1][m-1][false], step[n-1][m-1][true])<<ell;
}
/*CUSTOM TEST CASE 1
5 4
1 3 0 0
4 1 1 0
4 4 1 0
3 1 0 0
2 1 3 1
*/
/*CUSTOM TEST CASE 2
*/
/*CUSTOM TEST CASE 3
*/
/*Comments of Shame
- floating error (use integer division instead)
- cin vs getline
- upperbound and lowerbound returns iteratorsf
- use long long when number is big enough
- for dp invalid cases needs to be skipped
- some base cases won't work (review cow poetry)
- always check bounds, some TLE are due to incorrect bounds!
- dont mess up return types
= RESET ARRAYS!!!!!!!!!!
- USE UR BRAIN
- INF setting problems
- push vs pb
- check if things are used properly
- read the PROBLEM (directed vs undirected graph)
*/
/*
freopen("time.in","r", stdin);
freopen("time.out","w", stdout);
*/