24h購物| | PChome| 登入
2013-07-18 11:38:52| 人氣277| 回應0 | 上一篇 | 下一篇

[UVA][greedy] 12498 - Ant's Shopping Mall

推薦 0 收藏 0 轉貼0 訂閱站台

BGC TRUST IUPC 2012

 

Problem E

Ant’s Shopping Mall

 

 

In the world of ant there is a popular shopping mall named Ant’s Shopping Mall. The shopping mall is a grid with R rows and C columns.  Any cell of the grid can be occupied by a shop or empty. The shopping mall is dynamic in a sense that if there is a shop at cell (r, c) (where 1 <= r <= R and 1 <= c <= C, here r defines the row and c defines the column of the cell) then in a single move the shop can be moved to (r, c - 1) or (r, c + 1) if the position is empty. But it cannot be moved outside the grid.

 

The ant queen now wants to visit the shopping mall. The queen can move vertically that is if the queen is at cell (r, c) then in the next step she can be at (r + 1, c). She has a special way of visit, she always starts from first row that is from any cell (1, c) and continues until the last row is reached that is cell (R, c). The owner of the shopping mall wants to impress queen so he wants to find a path of the form (1, c), (2, c), …, (R, c) for the queen in advance such that there is no shop in any cell of the path.  For this the owner of the mall may need to move zero or more shops but he wants to do it in minimum number of shop movement. Now the owner hired you to solve the task for him. If it is not possible to find a path then you have to report it also.

 

 

Input


First line of the input contains a positive number T, number of test cases. There will be at most 50 test cases. For each test case the first line contains R and C separated by spaces (2 <= R <= 50 and 1 <= C <= 50). Each of the next R lines contains C characters, each of them is either 0 or 1.  If the j-th character in the i-th line contains 1 then cell (i, j) contains a shop otherwise cell (i, j) is empty.

 

 

Output


For each test you have to output minimum number of moves needed if it is possible to generate a path for the queen. Otherwise output -1. See output format for clarification.

 

Sample Input

Output for Sample Input

3
2 4                    
1010                   
0101                   
3 3

111
111
111
3 5

01111
11110
11011

Case 1: 1

Case 2: -1

Case 3: 4

 

 

 

Problemsetter: Kazi Rakibul Hasan

Special Thanks: Md. Towhidul Islam Talukder

房子只能往左往右移動,而女王則是選定一個 column 開始從上由下移動。

移動的時候如果撞道障礙物,將房子往左推或往右推,選擇一邊花費較少的即可。


#include <stdio.h>
#include <algorithm>
using namespace std;

char g[105][105];
int main() {
    int testcase, cases = 0;
    scanf("%d", &testcase);
    while(testcase--) {
        int n, m, i, j, k, p, q, r;
        scanf("%d %d", &n, &m);
        for(i = 0; i < n; i++)
            scanf("%s", g[i]);
        int ret = 0xfffffff;
        for(i = 0; i < m; i++) {//brute force
            int sub = 0;
            for(j = 0; j < n; j++) {
                int left = 0xfffffff, right = 0xfffffff;
                if(g[j][i] == '0')  continue;
                p = i;
                while(p >= 0 && g[j][p] == '1') p--;
                q = i;
                while(q < m && g[j][q] == '1') q++;
                if(p >= 0)  left = i-p;
                if(q < m)   right = q-i;
                if(left == 0xfffffff && right == 0xfffffff)
                    break;
                sub += min(left, right);
            }
            if(j == n)
                ret = min(ret, sub);
        }
        if(ret == 0xfffffff)
            ret = -1;
        printf("Case %d: %d\n", ++cases, ret);
    }
    return 0;
}


台長: Morris
人氣(277) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][bitmask] 12515 - Movie Police
此分類上一篇:[UVA] 11975 - Tele-loto

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文