One Bomb
题目描述:
You are given a description of a depot. It is a rectangular checkered field of n × msize. Each cell in a field can be empty (".") or it can be occupied by a wall ("*").
You have one bomb. If you lay the bomb at the cell (x, y), then after triggering it will wipe out all walls in the row x and all walls in the column y.
You are to determine if it is possible to wipe out all walls in the depot by placing and triggering exactly one bomb. The bomb can be laid both in an empty cell or in a cell occupied by a wall.
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and columns in the depot field.
The next n lines contain m symbols "." and "*" each — the description of the field. j-th symbol in i-th of them stands for cell (i, j). If the symbol is equal to ".", then the corresponding cell is empty, otherwise it equals "*" and the corresponding cell is occupied by a wall.
If it is impossible to wipe out all walls by placing and triggering exactly one bomb, then print "NO" in the first line (without quotes).
Otherwise print "YES" (without quotes) in the first line and two integers in the second line — the coordinates of the cell at which the bomb should be laid. If there are multiple answers, print any of them.
3 4 .*.. .... .*..
YES 1 2
3 3 ..* .*. *..
NO
6 5 ..*.. ..*.. ***** ..*.. ..*.. ..*..
YES 3 3
题目意思是在一个矩阵中找到合适的点,满足所有 * 都在这个点的行和列上。
虽然是 1000 X 1000 , 暴力有点悬,但是还是可以过的( 30ms ....... 不敢相信 )。
输入矩阵的时候做一下统计,x数组记录所有行上的墙的数目,y 数组记录列上的墙的数目。
同时,累计墙的总数。
枚举矩阵每一个点,如果这个点的行和列上的墙(*)总数恰好等于总墙数,说明这个点符合条件,
炸弹放在这里可以炸掉所有的墙。
而且,可以先判断一下数组中是否有墙 ? cnt == 0 , 直接输出一个,不用搜索了。
输出注意下标从 1 开始。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std ;
char text[1001][1001] ;
int x[1001] , y[1001] ;
int main(){
int n , m , cnt = 0 ;
scanf( "%d%d" , &n , &m ) ;
for( int i = 0 ; i < n ; ++i ){
scanf( "%s" , text[i] ) ;
for( int j = 0 ; j < m ; ++j )
if( text[i][j] == '*' )
x[i]++ , y[j]++ , cnt++ ; // cnt 统计墙的个数
}
if( cnt == 0 ){ cout << "YES" << endl << "1 1" << endl ; return 0 ; }
for( int i = 0 ; i < n ; ++i )
for( int j = 0 ; j < m ; ++j ){
int ans = text[i][j] == '*' ? x[i]+y[j]-1 : x[i]+y[j] ;
if( ans == cnt ){
cout << "YES" << endl ; cout << i+1 << " " << j+1 << endl ;
return 0 ;
}
}
cout << "NO" << endl ;
return 0 ;
}
这题,我一开始不想暴力的,想通过统计 x , y 数组,找出 x 数组和 y 数组最大的值所在的行和列,交叉点是符合条件的,那样就不用搜索1000*1000的矩阵了。 可惜一直没过,卡在第 10 组数据上,可惜上 codeforces 看不到完整的第十组数据。以后过了,再补上吧。