Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<queue>
- using namespace std;
- int n,m;
- struct pos{
- int I,J;
- pos operator + (const pos& rhs)const
- {
- return {I + rhs.I , J + rhs.J};
- }
- };
- bool IsIb(pos a){
- return (0 <= a.I && a.I < n && 0 <= a.J && a.J < m);
- }
- struct elem{
- pos ps;
- int dis;
- bool operator < (const elem& rhs)const
- {
- return dis > rhs.dis;
- }
- };
- int main()
- {
- scanf("%d %d",&n,&m);
- char board[n][m+1];
- for(int i = 0 ; i < n ; i ++){
- scanf(" %s",board[i]);
- }
- pos Xpos[110];
- pos Apos[110];
- int xn = 0 , an = 0;
- priority_queue<elem> pq;
- int dist[n][m];
- bool visited[n][m];
- for(int i = 0 ; i < n ; i ++){
- for(int j = 0 ; j < m ; j ++){
- visited[i][j] = false;
- dist[i][j] = 1e9;
- if(board[i][j] == 'X'){
- Xpos[xn++] = {i,j};
- pq.push({i,j,0});
- dist[i][j] = 0;
- }
- else if(board[i][j] == 'A'){
- Apos[an++] = {i,j};
- }
- }
- }
- while(!pq.empty()){
- pos u = pq.top().ps;
- pq.pop();
- if(visited[u.I][u.J])continue;
- visited[u.I][u.J] = true;
- // printf("Test u : (%d,%d)\n",u.I,u.J);
- pos move[4] = {{-1,0},{0,1},{1,0},{0,-1}};
- for(int i = 0 ; i < 4 ; i ++){
- pos v = u + move[i];
- if(IsIb(v) && board[v.I][v.J] != 'W' && !visited[v.I][v.J] && dist[u.I][u.J] + 1 < dist[v.I][v.J] ){
- dist[v.I][v.J] = dist[u.I][u.J] + 1;
- pq.push({v,dist[v.I][v.J]});
- }
- }
- }
- int cnt = 0;
- int ans = 0;
- // printf("Test an = %d\n",an);
- for(int i = 0 ; i < an ; i ++){
- pos a = Apos[i];
- int da = dist[a.I][a.J];
- // printf("Test #%d : %d\n",i,da);
- if(da != 1e9){
- ans += da * 2;
- cnt++;
- }
- }
- printf("%d %d",cnt,ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement