-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMicrosoft_three.cpp
More file actions
103 lines (91 loc) · 2.53 KB
/
Microsoft_three.cpp
File metadata and controls
103 lines (91 loc) · 2.53 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
#include<iostream>
#include<queue>
#include<limits.h>
using namespace std;
const int DOWN=1;
const int RIGHT=0;
struct State{
int x,y;
int cost;
int dir;
State(int _x,int _y, int _cost, int _dir):x(_x),y(_y),cost(_cost),dir(_dir){}
State():x(0),y(0),cost(0),dir(RIGHT){};
bool operator < (const State & arg) const
{
return this->cost > arg.cost;
}
};
int main()
{
int n,m;
char graph[110][110];
int vis[110][110][2];
while(cin >>n>>m){
priority_queue<State> q;
//cout << n << " " << m << endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++){
cin >> graph[i][j];
vis[i][j][0]=INT_MAX;
vis[i][j][1]=INT_MAX;
}
//cout << graph[i] << endl;
}
vis[0][0][1] = 0;
vis[0][0][0] = 0;
State s;
s.x=0;
s.y=0;
s.cost=0;
s.dir=RIGHT;
q.push(s);
while(!q.empty()){
State now = q.top();
int x = now.x;
int y = now.y;
q.pop();
if(x == n - 1 && y == m - 1){
cout << now.cost << endl;
break;
}
State down;
down.dir = DOWN;
if(x + 1 < n){
down.cost = now.cost;
if(now.dir == RIGHT && graph[x][y + 1] == '.' && (y + 1) < m){
down.cost += 1;
}
if(graph[x+1][y] == 'b'){
down.cost += 1;
}
if(down.cost < vis[x + 1][y][DOWN])
{
down.x = x + 1;
down.y = y;
vis[x + 1][y][DOWN] = down.cost;
q.push(down);
}
}
State right;
right.dir = RIGHT;
if(y + 1 < m){
right.cost = now.cost;
if(now.dir == DOWN && graph[x + 1][y] == '.' && (x + 1) < n){
right.cost += 1;
}
if(graph[x][y+1]=='b'){
right.cost += 1;
}
if(right.cost < vis[x][y+1][RIGHT])
{
right.x = x;
right.y = y+1;
vis[x][y+1][RIGHT] = right.cost;
q.push(right);
}
}
}
}
return 0;
}