标签:make 方向 string map http tac mem push second
题目链接:https://www.luogu.org/problem/P1443
思路:
这题主要是这??可以走的地方太多了,需要考虑清楚每一种情况,方向数组不要有遗漏
1 #include <math.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <iostream> 5 #include <algorithm> 6 #include <string> 7 #include <string.h> 8 #include <vector> 9 #include <map> 10 #include <stack> 11 #include <set> 12 #include <queue> 13 14 15 #define LL long long 16 #define INF 0x3f3f3f3f 17 #define ls nod<<1 18 #define rs (nod<<1)+1 19 const int maxn = 500; 20 const double eps = 1e-9; 21 22 int n,m,sx,sy; 23 int dis[maxn][maxn]; 24 int dirx[9] = {0,2,-2,2,-2,-1,1,-1,1},diry[9]={0,1,1,-1,-1,2,2,-2,-2}; 25 26 std::queue<std::pair<int,int> > q; 27 28 int main() { 29 scanf("%d%d%d%d",&n,&m,&sx,&sy); 30 memset(dis,-1, sizeof(dis)); 31 dis[sx][sy] = 0; 32 q.push(std::make_pair(sx,sy)); 33 while (!q.empty()) { 34 int x = q.front().first; 35 int y = q.front().second; 36 q.pop(); 37 for (int i=1;i<=9;i++) { 38 int new_x = x + dirx[i]; 39 int new_y = y + diry[i]; 40 if (new_x >= 1 && new_x <= n && new_y >= 1 && new_y <= m && dis[new_x][new_y] == -1) { 41 dis[new_x][new_y] = dis[x][y] + 1; 42 q.push(std::make_pair(new_x,new_y)); 43 } 44 } 45 } 46 for(int i = 1;i <= n; ++i) { 47 for(int j = 1;j <= m; ++j) 48 printf("%-5d", dis[i][j]); 49 printf("\n"); 50 } 51 return 0; 52 }
标签:make 方向 string map http tac mem push second
原文地址:https://www.cnblogs.com/-Ackerman/p/11862558.html