公司的程序员不够用了,决定把产品经理都转变为程序员以解决开发时间长的问题。
在给定的矩形网格中,每个单元格可以有以下三个值之一:
- 值0代表空单元格;
- 值1代表产品经理;
- 值2代表程序员;
每分钟,任何与程序员(在4个正方向上)相邻的产品经理都会变成程序员。
返回直到单元格中没有产品经理为止所必须经过的最小分钟数。
如果不可能,返回-1。
以下是一个4分钟转变完成的示例:
2 1 1 2 2 1 2 2 2 2 2 2 2 2 2
1 1 0 -> 2 1 0 -> 2 2 0 -> 2 2 0 -> 2 2 0
0 1 1 0 1 1 0 1 1 0 2 1 0 2 2
输入格式
不固定多行(行数不超过10),毎行是按照空格分割的数字(不固定,毎行数字个数不超过10)。
其中每个数组项的取值仅为0、1、2三种。
读取时可以按行读取,直到读取到空行为止,再对读取的所有行做转换处理。
输出格式
如果能够将所有产品经理变成程序员,则输出最小的分钟数。
如果不能够将所有的产品经理变成程序员,则返回-1.
输入样例1:
0 2
1 0
输出样例1:
-1
输入样例2:
1 2 1
1 1 0
0 1 1
输出样例2:
3
输入样例3:
1 2
2 1
1 2
0 1
0 1
1 1
输出样例3:
4
这道题目首先输入输出有一些繁琐,用C++的话,先用getline读取一行存入字符串,然后用stringstream从字符串中读取数据,之后是一个多源点BFS问题,先将所有编号为2的点加入队列,开始BFS。
#include <iostream>
#include <string>
#include <queue>
#include <algorithm>
#include <cstring>
#include <sstream>
using namespace std;
const int N = 15;
int n, m; // n表示行,m表示列
int dist[N][N]; // 距离矩阵
int g[N][N]; // 图
typedef pair<int,int> PII;
int bfs()
{
queue<PII> que;
memset(dist,-1,sizeof(dist));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(g[i][j]==2){
dist[i][j] = 0;
que.push({i,j});
}
}
}
const int dx[] = {0,1,-1,0};
const int dy[] = {1,0,0,-1};
while(!que.empty()){
auto t = que.front();
que.pop();
int x = t.first, y = t.second, d = dist[x][y];
for(int i=0;i<4;i++){
int a = x + dx[i];
int b = y + dy[i];
if(a>=0 && a<n && b>=0 && b<m && g[a][b]==1 && dist[a][b]==-1){
dist[a][b] = d+1;
que.push({a,b});
}
}
}
int res=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(g[i][j]==1 && dist[i][j]==-1){
return -1;
}
res = max(res,dist[i][j]);
}
}
return res;
}
int main()
{
string line;
while (getline(cin, line)) {
int k = 0;
stringstream ssin(line); // 先读取一行字符串,再从字符串里读取数据
while (ssin >> g[n][k])
k++;
m = k; // m是行数
n++; // n是列数
}
/*for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << g[i][j] << " ";
}
cout << endl;
}*/
cout << bfs() << endl;
}