SGU 479 Funny Feature spfa

来源:互联网 发布:成都黑马程序员 编辑:程序博客网 时间:2024/06/05 05:55

题目链接:点击打开链接

最短路。


#include <cstdio>#include <cstring>#include <queue>using namespace std;const int N = 205;const int dx[] = {-1, 1, 0, 0};const int dy[] = {0, 0, 1, -1};int a[N][N], x[N*N], y[N*N];bool vis[N][N];queue<pair<int, int> > q;int main() {int n, m;while(~scanf("%d%d", &n, &m)) {memset(vis, 0, sizeof vis);while(q.size()) q.pop();for(int i = 0; i < n; i ++) {for(int j = 0; j < m; j ++) {scanf("%d", &a[i][j]);if(a[i][j] == 1) {q.push(make_pair(i, j));vis[i][j] = 1;}}}//printf("%d\n", q.size());int top = 0;pair<int, int> t;while(q.size()) {t = q.front(); q.pop();int xx = t.first, yy = t.second;a[xx][yy] --;x[++top] = xx;y[top] = yy;if(a[xx][yy] < 0) {top = -2;break;}for(int i = 0; i < 4; i ++) {int nx = xx + dx[i], ny = yy + dy[i];if(nx < 0 || nx >= n || ny < 0 || ny >= m) continue;if(a[nx][ny] > 0) a[nx][ny] --;if(a[nx][ny] == 1 && !vis[nx][ny]) q.push(make_pair(nx, ny));else if(a[nx][ny] < 0) {top = -2;break;}}if(top == -2) break;}//printf("%d\n", top);for(int i = 0; i < n; i ++) {for(int j = 0; j < m; j ++) {if(a[i][j] > 0) {top = -2;break;}}}if(top == -2) puts("No solution");else {for(int i = top; i > 0; i --) {printf("%d %d\n", x[i] + 1, y[i] + 1);}}}return 0;}


0 0
原创粉丝点击