Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- const int N = (int) 5e2 + 123;
- const int INF = (int) 1e9 + 7;
- char a[N][N];
- int x[2], y[2];
- int n, m;
- int dx[4] = {0, -1, 0, 1};
- int dy[4] = {1, 0, -1, 0};
- int dp[N][N];
- int path[N][N];
- bool inBoard(int xx, int yy){
- return xx >= 0 && yy >= 0 && xx < n && yy < m;
- }
- int main() {
- #ifndef ONLINE_JUDGE
- freopen("/Users/Hippskill/desktop/prog/cpp/input", "r", stdin);
- #endif
- scanf("%d%d", &n, &m);
- for(int i = 0; i < 2; i++){
- scanf("%d %d", &x[i], &y[i]);
- x[i]--; y[i]--;
- }
- for(int i = 0; i < n; i++){
- for(int j = 0; j < m; j++){
- dp[i][j] = INF;
- path[i][j] = INF;
- }
- scanf("%s", a[i]);
- }
- vector<pair<int, int> > que;
- dp[x[0]][y[0]] = 0;
- que.push_back(make_pair(x[0], y[0]));
- for(int i = 0; i < que.size(); i++){
- pair<int, int> v = que[i];
- for(int k = 0; k < 4; k++){
- int xx = v.first + dx[k];
- int yy = v.second + dy[k];
- if(inBoard(xx, yy)){
- if(dp[xx][yy] > dp[v.first][v.second] + (a[xx][yy] != a[v.first][v.second])){
- dp[xx][yy] = dp[v.first][v.second] + (a[xx][yy] != a[v.first][v.second]);
- que.push_back(make_pair(xx, yy));
- }
- }
- }
- }
- if(dp[x[1]][y[1]] == INF){
- printf("0 0\n");
- return 0;
- }
- else{
- vector<pair<int, int> > q;
- q.push_back(make_pair(x[0], y[0]));
- path[x[0]][y[0]] = 0;
- for(int i = 0; i < q.size(); i++){
- pair<int, int> v = q[i];
- for(int k = 0; k < 4; k++){
- int xx = v.first + dx[k];
- int yy = v.second + dy[k];
- printf("dp[%d][%d] = %d, dp[%d][%d] = %d\n", xx, yy, dp[xx][yy], v.first, v.second, dp[v.first][v.second]);
- if(inBoard(xx, yy) && (dp[xx][yy] == dp[v.first][v.second] + (a[xx][yy] == a[v.first][v.second]))){
- path[xx][yy] = min(path[xx][yy], path[v.first][v.second]+1);
- que.push_back(make_pair(xx, yy));
- }
- }
- }
- printf("%d %d\n", path[ x[1] ][ y[1] ], dp[ x[1] ][ y[1] ]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement