close
題目取自於ITSA協作平台
題目:糖果分享
問題描述 :
兒童節前夕,老師把全班同學的座位排成格子狀,並且準備了一袋一袋的糖果要分給小朋友們。老師把一袋一袋的糖果放在某幾個同學的抽屜裡,拿到一袋糖果的同學必須而且只能把糖果分給他前後左右的同學。請寫一個程式幫老師檢查看看,是不是全班的同學都可以拿到糖果呢?
輸入說明 :
輸入檔中第一行為一個正整數N,代表共有幾組測試資料。之後接下來每筆資料的第一行為兩個數字n、m和L, 1 ≤ n ≤ 20, 1 ≤ m ≤ 20, 1 ≤ L ≤ 100,以空格隔開,表示座位共有n行、m列以及糖果共有L袋,第二行開始每行有2個數字x和y,以一個空格隔開,共有L行,每一組x、y代表糖果放在第x行、第y列的抽屜裡。
輸出說明 :
若全部的同學都可以拿到糖果,則輸出Y,否則輸出N。每筆測試資料輸出於一行。
範例 :
Sample Input: |
Sample Output: |
3 |
N |
Code:
- import java.util.Scanner;
- public class Main{
- public static void main(String[] args){
- Scanner sc=new Scanner(System.in);
- int a = sc.nextInt();
- for(int b=0;b<a;b++){
- int n = sc.nextInt();
- int m = sc.nextInt();
- int l = sc.nextInt();
- int[][] arr = new int[n+2][m+2];
- for(int c=0;c<l;c++){
- int x = sc.nextInt();
- int y = sc.nextInt();
- arr[x][y]=1;
- arr[x+1][y]=1;
- arr[x-1][y]=1;
- arr[x][y+1]=1;
- arr[x][y-1]=1;
- }
- int num=0;
- for(int c=1;c<n+1;c++){
- for(int d=1;d<m+1;d++){
- if(arr[c][d]==0){
- num++;
- break;
- }
- }
- }
- if(num==0)
- System.out.println("Y");
- else
- System.out.println("N");
- }
- }
- }
文章標籤
全站熱搜