Problem:


Score: 300 points

Problem Statement

We have a canvas divided into a grid with H rows and W columns. The square at the i-th row from the top and the j-th column from the left is represented as (i, j).

Initially, all the squares are white. square1001 wants to draw a picture with black paint. His specific objective is to make Square (i, j) black when s_{i, j}= #, and to make Square (i, j) white when s_{i, j}= ..

However, since he is not a good painter, he can only choose two squares that are horizontally or vertically adjacent and paint those squares black, for some number of times (possibly zero). He may choose squares that are already painted black, in which case the color of those squares remain black.

Determine if square1001 can achieve his objective.  

Constraints

H is an integer between 1 and 50 (inclusive).

W is an integer between 1 and 50 (inclusive).

For every (i, j) (1 \leq i \leq H, 1 \leq j \leq W), s_{i, j} is # or ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1} s_{1, 2} s_{1, 3} ... s_{1, W}
s_{2, 1} s_{2, 2} s_{2, 3} ... s_{2, W}
  :   :
s_{H, 1} s_{H, 2} s_{H, 3} ... s_{H, W}

Output

If square1001 can achieve his objective, print Yes; if he cannot, print No.

Sample Input 1

3 3
.#.
###
.#.

Sample Output 1

Yes

One possible way to achieve the objective is shown in the figure below. Here, the squares being painted are marked by stars.

Sample Input 2

5 5
#.#.#
.#.#.
#.#.#
.#.#.
#.#.#

Sample Output 2

No

square1001 cannot achieve his objective here.

Sample Input 3

11 11
...#####...
.##.....##.
#..##.##..#
#..##.##..#
#.........#
#...###...#
.#########.
.#.#.#.#.#.
##.#.#.#.##
..##.#.##..
.##..#..##.

Sample Output 3

Yes



Sample input:
3 3
.#.
###
.#.


Sample output:
Yes

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from bisect import bisect_left


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


h, w = LI()
grid = SR(h)
que = deque()


def dfs():
    for y in range(h):
        for x in range(w):
            if grid[y][x] == '#':
                for i, j in [(0,1),(1,0),(-1,0),(0,-1)]:
                    if 0<=y+i<h and 0<=x+j<w and grid[y+i][x+j] == '#':
                        break
                else:
                    return 'No'
    return 'Yes'



print(dfs())

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
da=[input() for i in range(h)]
for i in range(h):
    da[i]='.'+da[i]+'.'
sample='.'*(w+2)
da.insert(0,sample)
da.append(da)
ju=False
for i in range(h):
    for j in range(w):
        if(da[i+1][j+1]=='#'):
            if(not(da[i][j+1]=='#' or da[i+1][j]=='#' or da[i+1][j+2]=='#' or da[i+2][j+1]=='#')):
                print('No')
                ju=True
                break
    if(ju):
        break
if(not ju):
    print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [list(input()) for i in range(H)]
s = [[0] * W] + s + [[0] * W]

for i in range(H + 2):
    s[i] = [0] + s[i] + [0]

for i in range(H):
    for j in range(W):
        if s[i + 1][j + 1] == '#':
            if s[i][j + 1] != '#' and s[i + 2][j + 1] != '#' and s[i + 1][j] != '#' and s[i + 1][j + 2] != '#':
                print('No')
                exit()
        else:
            continue
print('Yes')

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]

dx=[0,1,0,-1]
dy=[1,0,-1,0]
ans=True
for i in range(h):
    for j in range(w):
        if s[i][j]=='#':
            ans=False
            for k in range(4):
                nx = i+dx[k]
                ny = j+dy[k]
                if nx>=0 and nx <h and ny>=0 and ny<w and s[nx][ny]=='#':
                    ans = True
                    break
            if ans ==False:
                print('No')
                exit()
if ans:
    print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int,input().split())
s = [list(input()) for _ in range(H)]
for i in range(H):
    s[i] = [0] + s[i] + [0]
s = [[0]*(W+2)] + s + [[0]*(W+2)]
for i in range(1,H+1):
    for j in range(1,W+1):
        if s[i][j] == '#':
            if s[i-1][j] == '#' or s[i][j-1] == '#' or s[i][j+1] == '#' or s[i+1][j] == '#':
                continue
            else:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
import sys
H,W = map(int,input().split())
S = ["."*(W+2)]
for _ in range(H):
    s = input()
    s = "."+s+"."
    S.append(s)
S.append("."*(W+2))

for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j] == "#":
            if S[i+1][j] == S[i-1][j] == S[i][j+1] == S[i][j-1] == ".":
                print("No")
                sys.exit()
print("Yes")

Submission status: Accepted

Solution code:
h, w = map(int,(input().split()))
s= [[0]*(w+2)]*(h+2)
ans= 'Yes'
for i in range(1,h+1):
        s[i] = [0] + list(input()) + [0]

for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == '#':
            if s[i+1][j]=='#' or s[i-1][j]=='#' or s[i][j+1]=='#' or s[i][j-1]=='#':
                pass
            else:
                ans='No'
                break

print(ans)

Submission status: Accepted

Solution code:
def check():
    H, W = [int(i) for i in input().split(' ')]
    a = [input() for _ in range(H)]
    
    
    for h in range(H):
        for w in range(W):
            flag = [0,0,0,0]
            if a[h][w] == '#':
                if h != 0:
                    if a[h-1][w] != '#':
                        flag[0] = 1
                else:
                    flag[0] = 1
                if h != H-1:
                    if a[h+1][w] != '#':
                        flag[1] = 1
                else:
                    flag[1] = 1
                if w != 0:
                    if a[h][w-1] != '#':
                        flag[2] = 1
                else:
                    flag[2] = 1
                if w != W-1:
                    if a[h][w+1] != '#':
                        flag[3] = 1
                else:
                    flag[3] = 1 
            if sum(flag) == 4:
                    return 'No'
                    
    return 'Yes'
print(check())

Submission status: Accepted

Solution code:
import sys

H, W = map(int, input().split())
S = ['.'*(W+2)] + ['.' + input() + '.' for _ in range(H)] + ['.'*(W+2)]
for i in range(1, H+1):
  for j in range(1, W+1):
    if S[i][j] == '#':
      for x, y in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
        if S[i+x][j+y] == '#':
          break
      else:
        print('No')
        sys.exit()
else:
  print('Yes')

Submission status: Accepted

Solution code:
def solve():
    H,W = list(map(int, input().split()))
    S = ['.'*(W+2)]+['.'+input()+'.' for _ in range(H)]+['.'*(W+2)]
    result = 'Yes'
    
    # print(S)
    
    for i in range(1,H+1):
        for j in range(1,W+1):
            if S[i][j] == '#':
                if not(S[i+1][j] == '#' or S[i-1][j] == '#' or S[i][j+1] == '#' or S[i][j-1] == '#'):
                    result = 'No'
                    break
    
    print( result )
    
solve()

Submission status: Accepted

Solution code:
#%%
from itertools import product

#%%
def resolve():
    h,w=map(int,input().split())
    A=['.'+input()+'.' for _ in range(h)]
    A=['.'*(w+2)]+A+['.'*(w+2)]
    flag = 1

    for i,j in product(range(1,h+1),range(1,w+1)):
        if A[i][j]=='#':
            flag = min(flag,A[i-1][j]=='#' or A[i+1][j]=='#' or A[i][j-1]=='#' or A[i][j+1]=='#')

    print("Yes" if flag else "No")
resolve()

Submission status: Accepted

Solution code:
#%%
from itertools import product

#%%
def resolve():
    h,w=map(int,input().split())
    A=['.'+input()+'.' for _ in range(h)]
    A=['.'*(w+2)]+A+['.'*(w+2)]
    flag = 1

    for i,j in product(range(1,h+2),range(1,w+2)):
        if A[i][j]=='#':
            flag = min(flag,A[i-1][j]=='#' or A[i+1][j]=='#' or A[i][j-1]=='#' or A[i][j+1]=='#')

    print("Yes" if flag else "No")
resolve()

Submission status: Accepted

Solution code:
from collections import deque
import sys
sys.setrecursionlimit(10**7)
#input = sys.stdin.readline
def solve():
  h,w = (int(i) for i in input().split())
  maze = ["."*(w+2)]
  for i in range(h):
    maze.append("." + input()+ ".")
  maze.append("."*(w+2))
  start = deque([])

  for i in range(h):
    for j in range(w):
      if maze[i][j] == "#":
        start.append((i,j))

  while start:
      height,width = start.popleft()
      isblacknear = False
      d = [(1,0),(0,1),(-1,0),(0,-1)]
      for dy,dx in d:
        nh = height + dy
        nx = width + dx
        if maze[nh][nx] == "#":
          isblacknear = True
          break
      if not isblacknear:
        print("No")
        exit()
  print("Yes")
        
      
      
solve()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = ["." * (w+2)] + ["." + input() + "." for _ in range(h)] + ["." * (w+2)]
for i in range(h):
  for j in range(w):
    if s[i][j] == "#":
      if s[i-1][j]=="." and s[i+1][j]=="." and s[i][j-1]=="." and s[i][j+1]==".":
        print("No")
        exit()
print("Yes")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())

temp = "0" * (w+2)

s = [temp]

for i in range(h):
    s.append("0" + input() + "0")

s.append(temp)

for i in range(h+2):
    for j in range(w+2):
        flag = False
        if s[i][j] == "#":
            if s[i-1][j] == "#" or s[i+1][j] == "#" or s[i][j+1] == "#" or s[i][j-1] == "#":
                flag = True
            if not flag:
                print("No")
                exit()

print("Yes")

Submission status: Accepted

Solution code:
from sys import exit
h, w = map(int, input().split())
s = [input() for _ in range(h)]
for i in range(h):
  for j in range(w):
    if s[i][j] == '.':
      continue
    if i > 0 and s[i - 1][j] == '#':
      continue
    if i < h - 1 and s[i + 1][j] == '#':
      continue
    if j > 0 and s[i][j - 1] == '#':
      continue
    if j < w - 1 and s[i][j + 1] == '#':
      continue
    print('No')
    exit()
print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())

m = []
for i in range(H):
    m.append(list(input().strip()))

for h in range(H):
    for w in range(W):
        if m[h][w] != '#':
            continue
        # left
        if w - 1 >= 0:
            if m[h][w - 1] == '#':
                continue
        # right
        if w + 1 < W:
            if m[h][w + 1] == '#':
                continue
        # top
        if h - 1 >= 0:
            if m[h - 1][w] == '#':
                continue
        # right
        if h + 1 < H:
            if m[h + 1][w] == '#':
                continue
        print('No')
        exit()

print('Yes')

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    h, w = list(map(int, input().rstrip('\n').split()))
    a = [list(str(input().rstrip('\n'))) for _ in range(h)]
    for i in range(h):
        for j in range(w):
            if a[i][j] == "#":
                cnt = 0
                if 0 <= i - 1 < h and a[i-1][j] == "#":
                    cnt += 1
                if 0 <= i + 1 < h and a[i+1][j] == "#":
                    cnt += 1
                if 0 <= j - 1 < w and a[i][j-1] == "#":
                    cnt += 1
                if 0 <= j + 1 < w and a[i][j+1] == "#":
                    cnt += 1
                if cnt == 0:
                    print("No")
                    exit()
    print("Yes")


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())

s = ["." * (w + 2)]
for i in range(h):
    s.append("." + input() + ".")
s.append("." * (w + 2))

ans = "Yes"
for h_i in range(1, h + 1):
    for w_i in range(1, w + 1):
        if s[h_i][w_i] == "#":
            if s[h_i-1][w_i] == "." and s[h_i+1][w_i] == "." and s[h_i][w_i-1] == "." and s[h_i][w_i+1] == ".":
                ans = "No"

print(ans)

Submission status: Accepted

Solution code:
#ABC 096 C Grid Repainting 2

#数字の入力
#外枠を作ってカウントをやりやすくする
h, w = map(int, input().split())
s = []
waku = "a"*(w+2)
s.append(waku)
for i in range(h):
    kari = "a" + input() + "a"
    s.append(kari)
s.append(waku)
#ここまでで枠付きの文字列を入力した。調べるべきはs[1~h][1~w]
#検索対象は(i-1,j),(i,j-1),(i+1,j),(i,j+1)
k = [-1,0,1,0]
l = [0,-1,0,1]
#s[i + k[0~3]][j + l[0~3]]で検索可能

#"#"マークがある上下左右に#マークがあればセーフ。孤立しているのが一つでもあるとアウト
flag = 1#1なら成功、0なら失敗
for i in range(1,h + 1):
    for j in range(1,w+1):
        count = 0
        if s[i][j] == "#":
            for m in range(4):#前後探しループ
                if s[i+k[m]][j+l[m]] == "#":
                    count = 1 #1つでも見つけたフラグを立てる
            if count == 0:
                flag = 0 #1度でもcountが0にならずに前後探しループが終わったらアウト
if flag == 0:
    print("No")
if flag == 1:
    print("Yes")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
canpas = [input() for _ in range(h)]

for h_i in range(h):
    for w_i in range(w):
        if canpas[h_i][w_i] == '#':
            if w_i - 1 < 0:
                c1 = False
            else:
                c1 = canpas[h_i][w_i-1] == '#'

            if h_i - 1 < 0:
                c2 = False
            else:
                c2 = canpas[h_i-1][w_i] == '#'

            if h_i + 1 >= h:
                c3 = False
            else:
                c3 = canpas[h_i+1][w_i] == '#'

            if w_i + 1 >= w:
                c4 = False
            else:
                c4 = canpas[h_i][w_i+1] == '#'

            if c1 or c2 or c3 or c4:
                continue
            else:
                print('No')
                break
    else:
        continue
    break
else:
    print('Yes')

Submission status: Accepted

Solution code:
"""考え方
上下左右のいずれか隣接するマスが黒である必要がある。
つまり黒マスが1つだけポツンと存在した場合はNGとなる。

横チェック、縦チェック、の2つに分断して確認を行う。
"""

H,W = map(int,input().split())

list_matu = []

for h in range(H):
  list_matu.append(list(input()))


## 横チェック
ng_matu=[]
for h in range(H): ## 全レコードが対象
  for w in range(1,W-1): ## 列ごとに実施
    target = list_matu[h][w]
    target_left = list_matu[h][w-1]
    target_right = list_matu[h][w+1]
    
    if target=='#':
      if (target_left != '#') and (target_right != '#'):
        ng_matu.append([h,w])


## 縦チェック
for w in range(W):
  for h in range(1,H-1):
    target = list_matu[h][w]
    target_over = list_matu[h+1][w]
    target_under = list_matu[h-1][w]
    
    if target=='#':
      if (target_over != '#') and (target_under != '#'):
        if [h,w] in ng_matu:
          print('No')
          exit()


print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
Slist = list()
HWlist =list()

for i in range(0,H):
    Slist.append(str(input()))
    HWlist.append(list(Slist[i]))

for i in range(0,H):
	for j in range(0,W):
		frag = 0
		if HWlist[i][j] == "#":
			if i-1 >=0:
				if HWlist[i-1][j]=="#":
					frag = frag +1
			if i + 1 < H:
				if HWlist[i + 1][j] == "#":
					frag = frag + 1
			if j - 1 >= 0:
				if HWlist[i][j-1] == "#":
					frag = frag + 1
			if j + 1 < W:
				if HWlist[i][j+1] == "#":
					frag = frag + 1
			if frag == 0:
				print("No")
				exit()

print("Yes")

Submission status: Accepted

Solution code:
def main():
    H, W = map(int, input().split())
    board = [list(input()) for i in range(H)]

    check = [[False] * W for i in range(H)]

    count = 0
    for i in range(H):
        for j in range(W):
            if not check[i][j]:
                if board[i][j] == ".":
                    check[i][j] = True
                    count += 1
                else:
                    ok = False
                    for tx, ty in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
                        if 0 <= tx and tx < H and 0 <= ty and ty < W:
                            if board[tx][ty] == "#":
                                ok = True
                                if not check[tx][ty]:
                                    check[tx][ty] = True
                                    count += 1
                    if ok:
                        check[i][j] = True
                        count += 1
    if count == H*W:
        print("Yes")
    else:
        print("No")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
table = [[False for _ in range(w + 2)]]
for i in range(h):
    row = [False]
    for c in input():
        row.append(c == '#')
    row.append(False)
    table.append(row)
table.append([False for _ in range(w + 2)])

ok = True
for i in range(1, h + 1):
    for j in range(1, w + 1):
        if table[i][j]:
            ok = ok and (table[i][j - 1] or table[i][j + 1] or table[i - 1][j]
                         or table[i + 1][j])
        if not ok:
            break
    if not ok:
        break
if ok:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
import sys
import math
from fractions import gcd
# import queue
# from collections import Counter
# from itertools import accumulate
# from functools import reduce

def lcm(a, b):
    return a * b // gcd(a, b)

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""
# 標準入力取得
## 文字列
 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())

## 数値
 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""
# 塗ることができないケースを考察してみる
# 愚直にシミュレーションしてもいけるかもしれない

H, W =  map(int, sys.stdin.readline().split())
L =  [list(list(sys.stdin.readline().rstrip())) for i in range(H)]

PL = [["0"] + l + ["0"] for l in L]

t = ["0"] * (W + 2)

PL.insert(0, t)
PL.append(t)

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if PL[i][j] == "#":
            if PL[i][j - 1] == "#" or PL[i - 1][j] == "#" or PL[i][j + 1] == "#" or PL[i + 1][j] == "#":
                continue
            else:
                print("No")
                exit()

print("Yes")

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
G = []
for i in range(H):
  G.append(list(input()))

for i in range(H):
  for j in range(W):
    if G[i][j] == '#':
      if i == 0:
        if j == 0:
          if G[0][1] == '.' and G[0][0] == '.':
            print("No")
            exit()
        if 0<j<W-1:
          if G[0][j-1] == '.' and G[0][j+1] == '.' and G[1][j] == '.':
            print("No")
            exit()
        if j == W-1:
          if G[0][j-1] == '.' and G[1][j] == '.':
            print("No")
            exit()
      elif 0<i<H-1:
        if j==0:
          if G[i-1][j] == '.' and G[i][1] == '.' and G[i+1][0] == '.':
            print("No")
            exit()
        elif 0<j<W-1:
          if G[i-1][j] == '.' and G[i][j-1] == '.' and G[i][j+1] == '.' and G[i+1][j] == '.':
            print("No")
            exit()
        elif j == W-1:
          if G[i-1][j] == '.' and G[i][j-1] == '.' and G[i+1][j] == '.':
            print("No")
            exit()
      elif i == H-1:
        if j == 0:
          if G[H-2][j] == '.' and G[H-1][j+1] == '.':
            print("No")
            exit()
        elif 0<j<W-1:
          if G[i-1][j] == '.' and G[i][j-1] == '.' and G[i][j+1] == '.':
            print("No")
            exit()
        elif j == W-1:
          if G[i-1][j] == '.' and G[i][j-1] == '.':
            print("No")
            exit()
            
print("Yes")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
h, w = map(int, input().split())
s = [""]*h

for i in range(h):
    s[i] = list(input())

# 探索対象になっている上下左右のマス目との添字をあらかじめ記録する
dx = [-1, 0, 0, 1]
dy = [0, -1, 1, 0]

result = True
# 上下左右いずれかで2連続に塗り対象とできるマス
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            continue
        else:
            can_drow = False
            for d in range(len(dx)):
                x = j + dx[d]
                y = i + dy[d]

                if not 0 <= x < w:
                    continue
                if not 0 <= y < h:
                    continue
                if s[y][x] == "#":
                    can_drow = True
                    break
            if not can_drow:
                result = False

if result:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
import sys
h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == "#" and (i == 0 or s[i-1][j] == ".") and (i == h-1 or s[i+1][j] == ".") and (j == 0 or s[i][j-1] == ".") and (j == w-1 or s[i][j+1] == "."):
            print("No")
            sys.exit()
print("Yes")

Submission status: Accepted

Solution code:
import sys
h,w = map(int,input().split())
s = ['']*h
for i in range(h):
  s[i] = input()
 
black = False
for i in range(h):
  for j in range(w):
    if s[i][j] == '#':
      if j != w - 1:
        if s[i][j + 1] == '#':
          continue
      if  j != 0:
        if s[i][j - 1] == '#':
          continue
      if i != h - 1:
        if s[i + 1][j] == '#':
          continue
      if i != 0:
        if s[i - 1][j] == '#':
          continue
      print('No')
      sys.exit()       
print('Yes')

Submission status: Accepted

Solution code:
import sys

H, W = map(int,input().split())
canv = [list(map(str, input())) for _ in range(H)]

def judge(f, i, j):
	np = [[i-1,j], [i+1,j], [i,j-1], [i,j+1]]
	for p in np:
		if 0 <= p[0] <= H-1 and 0 <= p[1] <= W-1 and f[p[0]][p[1]] =="#":
			return 0
	return 1


for i in range(H):
	for j in range(W):
		if canv[i][j] == "#":
			if judge(canv, i, j) == 1:
				print("No")
				sys.exit()

print("Yes")

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

h, w = na()
li = []
pad = ""
for i in range(w+2):
    pad += "."
li.append(pad)
for i in range(h):
    li.append("." + input() + ".")
li.append(pad)
ans = "Yes"
flag = False
for y in range(1, h+1):
    for x in range(1, w+1):
        if li[y][x] == "#":
            if li[y-1][x] == "#" or li[y+1][x] == "#" or li[y][x-1] == "#" or li[y][x+1] == "#":
                continue
            else:
                ans = "No"
                flag = True
                break
    if flag:
        break
print(ans)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = []
for i in range(H):
  ss = input()
  S.append(ss)
ok = True
for i in range(H):
  for j in range(W):
    if S[i][j] == "#":
      ok2 = False
      if j-1 >= 0 and S[i][j-1] == "#":
        ok2 = True
      if i-1 >= 0 and S[i-1][j] == "#":
        ok2 = True
      if j+1 < W and S[i][j+1] == "#":
        ok2 = True
      if i+1 < H and S[i+1][j] == "#":
        ok2 = True
      if not(ok2):
        ok = False
if ok:
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

from itertools import product

H,W = II()
s = [list(map(str, input())) for _ in range(H)]

def neighbor(i,j):
    if H==1:
        x = []
    else:
        if i==0:
            x = [1]
        elif i==H-1:
            x = [H-2]
        else:
            x = [i-1,i+1]
    if W==1:
        y = []
    else:
        if j==0:
            y = [1]
        elif j==W-1:
            y = [W-2]
        else:
            y = [j-1,j+1]
    return  list(product(x,[j])) + list(product([i],y))  

for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            flag = True
            a = neighbor(i,j)
            for k in a:
                if s[k[0]][k[1]] == '#':
                    flag = False
                    break
            if flag:
                print('No')
                exit()

print('Yes')

Submission status: Accepted

Solution code:
# 2019-08-27 22:59:05
H, W = list(map(int, input().split()))

S = []
for i in range(H):
    s = list(input())
    S.append(s)
f = "Yes"
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            r = 0
            if i != 0:
                if S[i - 1][j] == "#":
                    r += 1
            if j != 0:
                if S[i][j - 1] == "#":
                    r += 1
            if i + 1 != H:
                if S[i + 1][j] == "#":
                    r += 1
            if j + 1 != W:
                if S[i][j + 1] == "#":
                    r += 1
            if r == 0:
                f = "No"
                break
    if f == "No":
        break

print(f)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
sss = [input() for _ in range(h)]

ok = 1
for i, ss in enumerate(sss) :
    for j, s in enumerate(ss) :
        if s == "#" :
            okness = 0
            try : 
                if ss[j-1] == "#" :
                    okness = 1
            except : pass
            try : 
                if ss[j+1] == "#" :
                    okness = 1
            except : pass
            try : 
                if sss[i-1][j] == "#" :
                    okness = 1
            except : pass
            try : 
                if sss[i+1][j] == "#" :
                    okness = 1
            except : pass
            ok *= okness
print(" YNeos"[ok::2])

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = ["."+input()+"." for _ in range(h)]
s = ["."*(w+2)]+s+["."*(w+2)]
flag = False

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            if s[i-1][j] == '.' and s[i+1][j] == '.' and s[i][j-1] == '.' and s[i][j+1] == '.':
                flag = True

if flag:
    print('No')
else:
    print('Yes')

Submission status: Accepted

Solution code:
h, w = list(map(int, input().split()))
s = [input() for _ in range(h)]
move = [(-1, 0), (1, 0), (0, -1), (0, 1)]
flag = 1
for i in range(h):
    for j in range(w):
        hantei = []
        if s[i][j] == "#":
            for dx, dy in move:
                if i+dx<0 or j+dy<0 or i+dx>h-1 or j+dy>w-1:
                    continue
                else:
                    hantei.append(s[i+dx][j+dy])
            if "#" not in hantei:
                flag = 0
                break

if flag:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
S=[["."]*(w+2)]+[["."]+list(input())+["."] for _ in range(h)]+[["."]*(w+2)]
ans="Yes"
for i in range(1,h+1):
  for j in range(1,w+1):
    if S[i][j]=="#" and S[i+1][j]=="." and S[i-1][j]=="." and S[i][j+1]=="." and S[i][j-1]==".":
      ans="No"
      break
print(ans)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [list(input()) for i in range(h)]

di = (0, 1, 0, -1) 
dj = (1, 0, -1, 0)

ans = 'Yes'
for i in range(h):
  if ans == 'No':
      break
  for j in range(w):
    if ans == 'No':
      break
    count = 0
    if s[i][j] == '#':
      for k in range(4):
        if i+di[k] < 0 or h <= i+di[k]:
          count += 1
          continue
        elif j+dj[k] < 0 or w <= j+dj[k]:
          count += 1
          continue
        else:
          if s[i+di[k]][j+dj[k]] == '#':
            break
          else:
            count += 1
    if count == 4:
      ans = 'No'
      break
          
print(ans)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
grid = [[0 for _ in range(w+2)] for _ in range(h+2)]
for j in range(1, h+1):
  cnt = 0
  for a in input():
    cnt+=1
    grid[j][cnt]= 1 if a!='.' else 0
grid[1][1]=3

for j in range(1,h+1):
  for i in range(1,w+1):
    if grid[j][i] == 1 and grid[j-1][i] == 0 and  grid[j+1][i] == 0 and grid[j][i-1] == 0 and  grid[j][i+1] == 0:
      print('No')
      exit()
print('Yes')

Submission status: Accepted

Solution code:
h, w = map(int, input().split())

s = []

s.append('.' * (w + 2))

for i in range(h):
  s.append('.' + input() + '.')
  
s.append('.' * (w + 2))
  
sol = 0
for i in range(1, h + 1):
  for j in range(1, w + 1):
    
    if s[i][j] == '#':
      if not '#' in [s[i-1][j], s[i+1][j], s[i][j-1], s[i][j+1]]:
        sol += 1
if sol > 0:
  print('No')
else:
  print('Yes')

Submission status: Accepted

Solution code:
h, w = list(map(int, input().split()))
s = [list(input()) for i in range(h)]
dir = [[-1, 0], [1, 0], [0, -1], [0, 1]]
for y in range(h):
    for x in range(w):
        can_put = True
        if s[y][x] == "#":
            for dx, dy in dir:
                cx = x + dx
                cy = y + dy
                if cx >= 0 and cx < w and cy >= 0 and cy < h:
                    if s[cy][cx] == "#":
                        can_put = True
                        break
            else:
                can_put = False
        if can_put == False:
            break
    else:
        continue
    break
if can_put == True:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
import sys
height, width = map(int, input().split())
array = [0 for i in range(height)]

for i in range(height):
    array[i] = list(input())

h = [-1, 0,0,1]
w = [ 0,-1,1,0]

for a in range(height):
    for b in range(width):
        if array[a][b] == "#":
            for i in range(4):
                y = a + h[i]
                x = b + w[i]
                if y < 0 or y >= height: continue
                if x < 0 or x >= width: continue
                if array[y][x] == "#":
                    break
            else:
                print("No")
                sys.exit()
print("Yes")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())

S = [['.']*(W+2)]

for i in range(H):
    s = input()
    S.append(['.'] + list(s) + ['.'])
    
S.append([['.']*(W+2)])

flag = True

for i in range(1, H):
    for j in range(1, W):
        if S[i][j]=='#':
            if S[i-1][j]!='#' and S[i][j-1]!='#' and S[i+1][j]!='#' and S[i][j+1]!='#':
                flag = False
                break
            
if flag:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
h, w = map(int, input().split(' '))
dot_map = [input() for _ in range(h)]

for i in range(h):
    for j in range(w):
        link = [(-1, 0), (1, 0), (0, -1), (0, 1)]
        if dot_map[i][j] == '#':
            count = 0
            for n, m in link:
                if 0<= i + n < h and 0 <= j + m < w and dot_map[i + n][j + m] == '#':
                    count += 1
            if count == 0:
                print('No')
                exit(0)

print('Yes')