#include #include #include #include using namespace std; vector> findourv; int a,b; char mat[55][55]; int bfs(vector> v, map>, bool>pos, char c) { //toa sto dvizime piano queue>>Q; Q.push(v); //cekori queueq; q.push(0); while(!q.empty()) { vector> vi=Q.front(); Q.pop(); int k=q.front(); q.pop(); if(vi==findourv) { return k; } bool t=true; for(int i=0;i=a) { t=false; break; } if(mat[vi[i].first +1][vi[i].second]=='#') { t=false; break; } if(isdigit(mat[vi[i].first +1][vi[i].second]) && mat[vi[i].first +1][vi[i].second]!=c) { t=false; break; } } if(t==true) { vector> nadole; nadole=vi; for(int i=0;i>najgore; najgore=vi; for(int i=0;i=b) { t=false; break; } if(mat[vi[i].first][vi[i].second + 1]=='#') { t=false; break; } if(isdigit(mat[vi[i].first][vi[i].second + 1]) && mat[vi[i].first][vi[i].second + 1]!=c) { t=false; break; } } if(t==true) { vector> desno; desno=vi; for(int i=0;i> levo; levo=vi; for(int i=0;i>b>>a; vector>v1; vector>v2; vector>v3; map>,bool> pos; for(int i=0;i>mat[i][j]; if(mat[i][j]=='1') { v1.push_back(make_pair(i,j)); } if(mat[i][j]=='2') { v2.push_back(make_pair(i,j)); } if(mat[i][j]=='3') { v3.push_back(make_pair(i,j)); } if(mat[i][j]=='F') { findourv.push_back(make_pair(i,j)); } } } pos[v1]=true; int p1=bfs(v1,pos,'1'); pos.clear(); pos[v2]=true; int p2=bfs(v2,pos,'2'); pos.clear(); pos[v3]=true; int p3=bfs(v3,pos,'3'); int najmal=1000000; if(p1