搜索
查看: 163|回复: 0

为什么一直都有错误啊

[复制链接]
 楼主| 发表于 2019-6-25 16:52:45 | 显示全部楼层 |阅读模式
5啊哈币
  1. #include <iostream>
  2. #include <cstring>
  3. #include <queue>
  4. using namespace std;

  5. typedef struct node{
  6.     int row;
  7.     int col;
  8.     int step;
  9.     node(int r,int c,int s){
  10.         row = r;
  11.         col = c;
  12.         step = s;
  13.     }
  14. }Point;

  15. int direct[][2] = {-1,0,
  16.                   1,0,
  17.                   0,-1,
  18.                   0,1};
  19. int buyer[1001][1001];
  20. int isvisited[1001][1001];
  21. int n,m,k,d;
  22. int x,y,c;
  23. int buyer_cnt;
  24. queue<Point> q;
  25. long long ans = 0;

  26. void bfs()
  27. {
  28.     Point front(0,0,0) ,v(0,0,0);
  29.     while(!q.empty()){
  30.         front = q.front();
  31.         q.pop();
  32.         for(int i = 0 ; i < 4 ; i++){
  33.             v.row = front.row+direct[i][0];
  34.             v.col = front.col+direct[i][1];
  35.             v.step = front.step+1;
  36.             if(v.row < 1 || v.row > n || v.col < 1 || v.col > n){
  37.                 continue;
  38.             }
  39.             if(isvisited[v.row][v.col]){
  40.                 continue;
  41.             }
  42.             if(buyer[v.row][v.col] > 0){
  43.                 isvisited[v.row][v.col] = 1;
  44.                 ans += buyer[v.row][v.col]*v.step;
  45.                 buyer_cnt--;
  46.                 if(buyer_cnt == 0){
  47.                     return;
  48.                 }
  49.             }
  50.             isvisited[v.row][v.col] = 1;
  51.             q.push(v);
  52.         }
  53.     }
  54. }

  55. int main()
  56. {
  57.     while(cin>>n>>m>>k>>d){
  58.         memset(buyer,0,sizeof(buyer));
  59.         memset(isvisited,0,sizeof(isvisited));
  60.         buyer_cnt = 0;
  61.         for(int i = 0 ; i < m ; i++){
  62.             cin>>x>>y;
  63.             isvisited[x][y] = 1;    //分店搜索时跳过
  64.             q.push(Point(x,y,0));
  65.         }
  66.         for(int i = 0 ; i < k ; i++){
  67.             cin>>x>>y>>c;
  68.             if(buyer[x][y] == 0){
  69.                 buyer_cnt++;
  70.             }
  71.             buyer[x][y] += c;
  72.         }
  73.         for(int i = 0 ; i < d ; i++){
  74.             cin>>x>>y;
  75.             isvisited[x][y] = 1;
  76.         }
  77.         bfs();
  78.         cout<<ans<<endl;
  79.     }


  80.     return 0;
  81. }
复制代码

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表