Crank
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 146 Accepted Submission(s): 69
Problem Description
Chev Chelios had his heart stolen from him, by the boss of the most dangerous gang in the city. His heart has been replaced with an artificial chargeable one!
Chelios was on mission of locating the boss of the gang to get his heart back since then without luck! Now the artificial heart’s battery lifetime is about to expire! Fortunately, he finally located the target but he needs your help to get there before his artificial heart stops beating!
Chelios decided to attack the boss building from the roof because all gates are heavily protected by gangsters. Chelios has the map of the gang block which shows the heights of all buildings within the block. The plan is that a helicopter will drop Chelios on the roof of one of the buildings on the boundary of the block during night. Then Chelios will get to the boss building by moving to adjacent buildings, vertically or horizontally. Chelios can only move to a building which has the same or smaller height as the current building (going up severely affects his damaged heart).
Given the gang building block map which shows the heights of all buildings in the block along with the boss building, write a program to help Chev Chelios determine the number of buildings on the boundary of the block he can be dropped by the helicopter at so that he would be able to reach the boss’s building without climbing!
Chelios was on mission of locating the boss of the gang to get his heart back since then without luck! Now the artificial heart’s battery lifetime is about to expire! Fortunately, he finally located the target but he needs your help to get there before his artificial heart stops beating!
Chelios decided to attack the boss building from the roof because all gates are heavily protected by gangsters. Chelios has the map of the gang block which shows the heights of all buildings within the block. The plan is that a helicopter will drop Chelios on the roof of one of the buildings on the boundary of the block during night. Then Chelios will get to the boss building by moving to adjacent buildings, vertically or horizontally. Chelios can only move to a building which has the same or smaller height as the current building (going up severely affects his damaged heart).
Given the gang building block map which shows the heights of all buildings in the block along with the boss building, write a program to help Chev Chelios determine the number of buildings on the boundary of the block he can be dropped by the helicopter at so that he would be able to reach the boss’s building without climbing!
Input
The first line of input contains an integer T, the number of test cases. T test cases follow, the first line of each test case contains two integers (1 <= R,C <= 10) the height and width of the building block. The second line contains two integers (1 <= A <=
R), and (1 <= B <= C), the coordinates of the boss building on the map. R lines follows; each line consists of C space separated integers representing the heights of all buildings. A height H of a building satisfies (1 <= H <= 1000).
Output
For each test case, print the number of buildings on the boundary of the block Chev Chelios can be dropped by the helicopter at so that he would be able to reach the boss’s building without climbing up! Follow the output format below.
Sample Input
2 3 3 2 2 1 7 3 2 6 3 3 5 4 2 2 2 1 2 7 2 6
Sample Output
Case #1: 1 Case #2: 4只能在边界进行搜索,水题。#include <iostream> #include <cstring> #include <cstdio> using namespace std; #pragma warning(disable : 4996) #define MAX 15 int r, c, a, b; int map[MAX][MAX]; bool flag; bool vis[MAX][MAX]; const int moves[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; void dfs(int x, int y) { if(x == a && y == b) { flag = true; return; } for(int i = 0; i < 4; i++) { int p = x + moves[i][0]; int q = y + moves[i][1]; if(p >= 1 && p <= r && q >= 1 && q <= c && map[p][q] <= map[x][y] && !vis[p][q]) { vis[p][q] = true; dfs(p, q); vis[p][q] = false; } } } int main() { freopen("in.txt", "r", stdin); int t, ans; cin >> t; for(int k = 1; k <= t; k++) { ans = 0; cin >> r >> c; cin >> a >> b; for(int i = 1; i <= r; i++) { for(int j = 1; j <= c; j++) { cin >> map[i][j]; } } memset(vis, false, sizeof(vis)); for(int i = 1; i <= r; i++) { for(int j = 1; j <= c; j++) { if(i == 1 || i == r || j == 1 || j == c) { flag = false; dfs(i, j); if(flag) { ans++; } } } } printf("Case #%d: %d\n", k, ans); } return 0; }