题意:给定N*M的矩阵,然后依次给出K个理想的点的坐标x,y,但是,如果前面的点有在x,y处,那么就按照题意所给定的规则,将点放在理想点坐标(x,y)附近的某个位置。 n, m, k (1 ≤ n, m ≤ 2000, 1 ≤ k ≤ min(n·m, 105)
规则如下:
1.the value of |x1 - x2| + |y1 - y2| is minimum
2.if the choice is not unique, then among the seats that satisfy the first condition, this person selects the one for which the value of x2 is minimum
3.if the choice is still not unique, among the seats that satisfy the first and second conditions, this person selects the one for which the value of y2 is minimum
分析:刚开始拿到这个题目,从给定的点(x1,y1)开始暴力地向外搜索没有被标记过的顶点,但是,显然,这样必定会TLE。我们大多都能想到单纯一个vis数组标记点(x,y)有没有被访问还不行,而且由|x1 - x2| + |y1 - y2|,基本都能想到用一个dist数组来记录当前(x,y)多少范围之内的数已经被标记了,那么我枚举的时候,那些距离小于dis[x][y]的点我就不用再进行枚举了。这里的dis[x][y]可以类似圆的半径的概念吧~~~另外,我们更新dis[x][y]的时候,还有一个地方也要注意优化,就是在新加入(x,y)这一点时,【PS:要知道此时dis[x][y]可是为1或者是0的哦~】,如果(x,y)附近的某个点已经被频繁访问过很多遍了,也就是说,这个时候,我就不需要d = dis[x][y]开始找了,而是可以从 dis[x + i][y + j] - abs(i) - abs(j) 开始找。有了这两个优化,这道题也就AC了~
/****************************>>>>HEADFILES<<<<****************************/ #include <queue> #include <vector> #include <cstdio> #include <string> #include <cstring> #include <iomanip> #include <iostream> #include <algorithm> using namespace std; /****************************>>>>>DEFINE<<<<<*****************************/ //#pragma comment(linker, "/STACK:1024000000,1024000000") #define FIN freopen("input.txt","r",stdin) #define rep(i,a,b) for(int i = a;i <= b;i++) #define MP(a,b) make_pair(a,b) #define PB(a) push_back(a) #define fst first #define snd second /****************************>>>>>>DEBUG<<<<<<****************************/ #define out(x) cout<<x<<" "; /****************************>>>>SEPARATOR<<<<****************************/ const int maxn = 2000 + 5; int N, M, K, dis[maxn][maxn];; bool vis[maxn][maxn]; void f(int x, int y) { for(int i = -1; i <= 1; i++) for(int j = -1; j <= 1; j++) if(x + i > 0 && x + i <= N && y + j > 0 && y + j <= M) dis[x][y] = max(dis[x + i][y + j] - abs(i) - abs(j), dis[x][y]); } int main() { // FIN; while(~scanf("%d %d %d", &N, &M, &K)) { memset(vis, false, sizeof(vis)); memset(dis, 0, sizeof(dis)); int x, y; for(int i = 0; i < K; i++) { scanf("%d %d", &x, &y); if(!vis[x][y]) { printf("%d %d\n", x, y); vis[x][y] = true; dis[x][y] = 1; continue; } else { f(x, y); //这一句不能少,也是一个优化。 bool suc = false; for(int &d = dis[x][y];; dis[x][y]++) { for(int xx = x - d, y1 = 0; xx <= x; xx++, y1++) { if(xx <= 0) continue; if(y - y1 > 0 && !vis[xx][y - y1]) { suc = vis[xx][y - y1] = true; dis[xx][y - y1] = 1; printf("%d %d\n", xx, y - y1); break; } else if(y + y1 <= M && !vis[xx][y + y1]) { suc = vis[xx][y + y1] = true; dis[xx][y + y1] = 1; printf("%d %d\n", xx, y + y1); break; } } if(suc) break; for(int xx = x, y1 = d; xx <= d + x; xx++, y1--) { if(xx > N) break; if(y - y1 > 0 && !vis[xx][y - y1]) { suc = vis[xx][y - y1] = true; dis[xx][y - y1] = 1; printf("%d %d\n", xx, y - y1); break; } else if(y + y1 <= M && !vis[xx][y + y1]) { suc = vis[xx][y + y1] = true; dis[xx][y + y1] = 1; printf("%d %d\n", xx, y + y1); break; } } if(suc) break; } } } // rep(i,1,N) rep(j,1,M) printf("%d%c",dis[i][j],j == M?'\n':' '); } return 0; }