Chef and Hasan went for skiing in a rectangular field, the field can be represented as a grid of N rows and M columns, rows are numbered from 1 to N starting from top, columns are numbered from 1 to M starting from left, each cell has a a number representing the height of the snow on this particular part of the field.
after skiing for a while in the field they decided to play a game, first hasan has to choose a set of cells S not necessarily adjacent and show this set to Chef.
after that, Chef has to choose exactly one cell d not necessarily from set S and tell it to Hasan.
finally, Hasan has to choose one cell from set S and start skiing from that cell and try to reach cell d that the chef just chose, if he couldn't then Chef wins, otherwise Hasan wins.
When Hasan is skiing, he can move from one cell to another iff both cells are adjacent (they share a side) and the height of destination cell is not more than the height of current cell.
it's obvious that it's easy for Hasan to win this game, because he can simply choose all cells to be in set S, but he want the set S to have minimum number of cells to prove that he is very skilled in skiing. knowing that both players play optimally, please help Hasan to find minimum number of cells to be in set S to guarantee the winning for him.
Input
First line contains integer T donating the number of test-cases.
First line of each test-case contains two integers n and m donating the number of rows and columns respectively.
Each of the following n lines contains m numbers donating the heights of the field
Output
For each test-case output a single line containing a single integer donating minimum number of cells has to be in set S so that Hasan guarantee winning
Constraints
Should contain all the constraints on the input data that you may have. Format it like:
- 1 ≤ T ≤ 100
- 1 ≤ N, M ≤ 1,000
- 1 ≤ sum of N in all test-cases ≤ 1,000
- 1 ≤ sum of M in all test-cases ≤ 1,000
- 1 ≤ height of snow ≤ 1,000,000,000
Example
Input: 2 3 3 1 2 2 2 1 1 2 1 1 3 3 3 2 2 2 1 1 2 1 1 Output: 2 1
Explanation
Example case 1. Hasan has to select at least two cells to be in S so that he guarantee he can reach all other cells starting from anyone of them, example of cells to choose (2,1) and (1,2)
Example case 2. if Hasan chose cell (1,1) to be in S then he can reach any cell starting from it and thus guarantee winning
题意:一个N*M的雪地,每个格子代表其高度。要求找出一个最小的集合S,使得集合S里面的点可以滑到雪地的任何地方。
思路:显然优先找最高的点,从它bfs下去就行了,使用优先队列优先搜索高的。
# include <bits/stdc++.h>
# define mp make_pair
# define A first
# define B second
# define pb push_back
# define PII pair<int,int>
using namespace std;
const int maxn = 1e3+3;
int a[maxn][maxn],n,m;
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
bool vis[maxn*maxn];
PII p[maxn*maxn];
void bfs(PII x)
{
priority_queue<PII>q;
q.push(x);
while(!q.empty())
{
int x = q.top().A, y = q.top().B;
q.pop();
for(int i=0; i<4; ++i)
{
int nx = y/m + dx[i];
int ny = y%m + dy[i];
if(nx < 0 || ny < 0 || nx >= n || ny >= m || vis[nx*m+ny] || a[y/m][y%m] < a[nx][ny]) continue;
vis[nx*m+ny] = true;
q.push(mp(a[nx][ny], nx*m+ny));
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int ans = 0;
memset(vis, false, sizeof(vis));
scanf("%d%d",&n,&m);
for(int i=0; i<n; ++i)
{
for(int j=0; j<m; ++j)
{
scanf("%d",&a[i][j]);
p[i*m+j] = mp(a[i][j], i*m+j);
}
}
sort(p, p+n*m, greater<PII>());
for(int i=0; i<n*m; ++i)
{
if(!vis[p[i].B])
{
++ans;
vis[p[i].B] = true;
bfs(p[i]);
}
}
printf("%d\n",ans);
}
return 0;
}