Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <cmath>
- #include <set>
- using namespace std ;
- const int maxSide = 1100 ;
- const int INF = 100000000 ;
- vector< vector<string> > color(maxSide, vector<string>(maxSide, "xxxx")) ;
- vector< vector<int> > dist(maxSide, vector<int>(maxSide, INF)) ;
- vector< vector<bool> > visit(maxSide, vector<bool>(maxSide, false)) ;
- int M, N ;
- int main()
- {
- ios::sync_with_stdio(false) ;
- cin.tie(0) ;
- string cur ;
- cin >> M >> N ;
- char ch1, ch2, ch3, ch4 ;
- for(int i = 1 ; i <= M ; i++)
- {
- for(int j = 1 ; j <= N ; j++)
- {
- cin >> ch1 >> ch2 >> ch3 >> ch4 ;
- color[i][j][0] = ch1 ;
- color[i][j][1] = ch2 ;
- color[i][j][2] = ch3 ;
- color[i][j][3] = ch4 ;
- //cout << color[i][j] << "\n" ;
- }
- }
- dist[1][1] = 0 ;
- pair<int, pair<int, int> > tmp ;
- tmp.first = 0, tmp.second.first = 1, tmp.second.second = 1 ;
- set< pair<int, pair<int, int> > > Q ;
- Q.insert(tmp) ;
- while(true)
- {
- tmp = *(Q.begin()) ;
- Q.erase(Q.begin()) ;
- int min_dist = tmp.first ;
- int cur_r = tmp.second.first ;
- int cur_c = tmp.second.second ;
- if(cur_r == M && cur_c == N)
- break ;
- if(visit[cur_r][cur_c])
- continue ;
- visit[cur_r][cur_c] = true ;
- char next_col ;
- int cur_pen = 0 ;
- // Right
- if(cur_c + 1 <= N)
- {
- next_col = color[cur_r][cur_c+1][3] ;
- if(color[cur_r][cur_c][0] == next_col) cur_pen = 1 ;
- else if(color[cur_r][cur_c][1] == next_col) cur_pen = 0 ;
- else if(color[cur_r][cur_c][2] == next_col) cur_pen = 3 ;
- else cur_pen = 2 ;
- if(min_dist + cur_pen < dist[cur_r][cur_c+1])
- {
- dist[cur_r][cur_c+1] = min_dist + cur_pen ;
- tmp.first = min_dist + cur_pen ;
- tmp.second.first = cur_r ;
- tmp.second.second = cur_c+1 ;
- Q.insert(tmp) ;
- }
- }
- // Left
- if(cur_c - 1 > 0)
- {
- next_col = color[cur_r][cur_c-1][1] ;
- if(color[cur_r][cur_c][0] == next_col) cur_pen = 3 ;
- else if(color[cur_r][cur_c][1] == next_col) cur_pen = 2 ;
- else if(color[cur_r][cur_c][2] == next_col) cur_pen = 1 ;
- else cur_pen = 0 ;
- if(min_dist + cur_pen < dist[cur_r][cur_c-1])
- {
- dist[cur_r][cur_c-1] = min_dist + cur_pen ;
- tmp.first = min_dist + cur_pen ;
- tmp.second.first = cur_r ;
- tmp.second.second = cur_c-1 ;
- Q.insert(tmp) ;
- }
- }
- // Down
- if(cur_r + 1 <= M)
- {
- next_col = color[cur_r+1][cur_c][0] ;
- if(color[cur_r][cur_c][0] == next_col) cur_pen = 2 ;
- else if(color[cur_r][cur_c][1] == next_col) cur_pen = 1 ;
- else if(color[cur_r][cur_c][2] == next_col) cur_pen = 0 ;
- else cur_pen = 3 ;
- if(min_dist + cur_pen < dist[cur_r+1][cur_c])
- {
- dist[cur_r+1][cur_c] = min_dist + cur_pen ;
- tmp.first = min_dist + cur_pen ;
- tmp.second.first = cur_r+1 ;
- tmp.second.second = cur_c ;
- Q.insert(tmp) ;
- }
- }
- // Up
- if(cur_r - 1 > 0)
- {
- next_col = color[cur_r-1][cur_c][2] ;
- if(color[cur_r][cur_c][0] == next_col) cur_pen = 0 ;
- else if(color[cur_r][cur_c][1] == next_col) cur_pen = 3 ;
- else if(color[cur_r][cur_c][2] == next_col) cur_pen = 2 ;
- else cur_pen = 1 ;
- if(min_dist + cur_pen < dist[cur_r-1][cur_c])
- {
- dist[cur_r-1][cur_c] = min_dist + cur_pen ;
- tmp.first = min_dist + cur_pen ;
- tmp.second.first = cur_r-1 ;
- tmp.second.second = cur_c ;
- Q.insert(tmp) ;
- }
- }
- }
- cout << dist[M][N] ;
- return 0 ;
- }
Add Comment
Please, Sign In to add comment