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:
def main():
    # 下、右、上、左
    dy = (1, 0, -1, 0)
    dx = (0, 1, 0, -1)
    H, W, *s = open(0).read().split()
    H = int(H)
    W = int(W)
    s = ["." + si + "." for si in s]
    s.insert(0, "." * (W + 2))
    s.insert(-1, "." * (W + 2))
    for y in range(1, H + 1):
        for x in range(1, W + 1):
            if s[y][x] == "#" and\
                    "." == s[y + dy[0]][x + dx[0]]\
                    == s[y + dy[1]][x + dx[1]]\
                    == s[y + dy[2]][x + dx[2]]\
                    == s[y + dy[3]][x + dx[3]]:
                print("No")
                return
    print("Yes")
    return


main()

Submission status: Accepted

Solution code:
def main():
    # 下、右、上、左
    dy = (1, 0, -1, 0)
    dx = (0, 1, 0, -1)
    H, W = map(int, input().split())
    s = ("." * (W + 2),)
    for i in range(H):
        s += ("." + input() + ".",)
    s += ("." * (W + 2),)
    for y in range(1, H + 1):
        for x in range(1, W + 1):
            if s[y][x] == "#" and\
                    "." == s[y + dy[0]][x + dx[0]]\
                    == s[y + dy[1]][x + dx[1]]\
                    == s[y + dy[2]][x + dx[2]]\
                    == s[y + dy[3]][x + dx[3]]:
                print("No")
                return
    print("Yes")
    return


main()

Submission status: Accepted

Solution code:
def main():
    # 下、右、上、左
    dy = (1, 0, -1, 0)
    dx = (0, 1, 0, -1)
    H, W = map(int, input().split())
    s = ["." * (W + 2)]
    s += ["." + input() + "." for i in range(H)]
    s.append("." * (W + 2))
    for y in range(1, H + 1):
        for x in range(1, W + 1):
            if s[y][x] == "#" and\
                    "." == s[y + dy[0]][x + dx[0]]\
                    == s[y + dy[1]][x + dx[1]]\
                    == s[y + dy[2]][x + dx[2]]\
                    == s[y + dy[3]][x + dx[3]]:
                print("No")
                return
    print("Yes")
    return


main()

Submission status: Accepted

Solution code:
def main():
    # 下、右、上、左
    dy = (1, 0, -1, 0)
    dx = (0, 1, 0, -1)
    H, W = map(int, input().split())
    dot = "." * (W + 2)
    s = [dot]
    s += ["." + input() + "." for i in range(H)]
    s.append(dot)
    for y in range(1, H + 1):
        for x in range(1, W + 1):
            if s[y][x] == "#" and\
                    "." == s[y + dy[0]][x + dx[0]]\
                    == s[y + dy[1]][x + dx[1]]\
                    == s[y + dy[2]][x + dx[2]]\
                    == s[y + dy[3]][x + dx[3]]:
                print("No")
                return
    print("Yes")
    return


main()

Submission status: Accepted

Solution code:
def main():
    # 下、右、上、左
    dy = [1, 0, -1, 0]
    dx = [0, 1, 0, -1]
    H, W = map(int, input().split())
    dot = "." * (W + 2)
    s = [dot]
    s += ["." + input() + "." for i in range(H)]
    s.append(dot)
    for y in range(1, H + 1):
        for x in range(1, W + 1):
            if s[y][x] == "#" and\
                    "." == s[y + dy[0]][x + dx[0]]\
                    == s[y + dy[1]][x + dx[1]]\
                    == s[y + dy[2]][x + dx[2]]\
                    == s[y + dy[3]][x + dx[3]]:
                print("No")
                return
    print("Yes")
    return


main()

Submission status: Accepted

Solution code:
def main():
    # 下、右、上、左
    dy = [1, 0, -1, 0]
    dx = [0, 1, 0, -1]
    H, W = map(int, input().split())
    s = ["." * (W + 2)]
    s += ["." + input() + "." for i in range(H)]
    s.append("." * (W + 2))
    for y in range(1, H + 1):
        for x in range(1, W + 1):
            if s[y][x] == "#" and\
                    "." == s[y + dy[0]][x + dx[0]]\
                    == s[y + dy[1]][x + dx[1]]\
                    == s[y + dy[2]][x + dx[2]]\
                    == s[y + dy[3]][x + dx[3]]:
                print("No")
                return
    print("Yes")
    return


main()

Submission status: Accepted

Solution code:
def main():
    # 下、右、上、左
    dy = [1, 0, -1, 0]
    dx = [0, 1, 0, -1]
    H, W = map(int, input().split())
    s = ["." * (W + 2)]
    s.extend(["." + input() + "." for i in range(H)])
    s.append("." * (W + 2))
    for y in range(1, H + 1):
        for x in range(1, W + 1):
            if s[y][x] == "#" and s[y + dy[0]][x + dx[0]] == "."\
                    and s[y + dy[1]][x + dx[1]] == "."\
                    and s[y + dy[2]][x + dx[2]] == "."\
                    and s[y + dy[3]][x + dx[3]] == ".":
                print("No")
                return
    print("Yes")
    return


main()

Submission status: Accepted

Solution code:
def main():
    # 下、右、上、左
    dy = [1, 0, -1, 0]
    dx = [0, 1, 0, -1]
    H, W = map(int, input().split())
    s = ["." * (W + 2)]
    s += ["." + input() + "." for i in range(H)]
    s.append("." * (W + 2))
    for y in range(1, H + 1):
        for x in range(1, W + 1):
            if s[y][x] == "#" and s[y + dy[0]][x + dx[0]] == "."\
                    and s[y + dy[1]][x + dx[1]] == "."\
                    and s[y + dy[2]][x + dx[2]] == "."\
                    and s[y + dy[3]][x + dx[3]] == ".":
                print("No")
                return
    print("Yes")
    return


main()

Submission status: Accepted

Solution code:
def main():
    # 下、右、上、左
    dy = [1, 0, -1, 0]
    dx = [0, 1, 0, -1]
    H, W = map(int, input().split())
    s = [["."] * (W + 2)]
    s += [["."] + list(input()) + ["."] for i in range(H)]
    s.append(["."] * (W + 2))
    for y in range(1, H + 1):
        for x in range(1, W + 1):
            if s[y][x] == "#" and s[y + dy[0]][x + dx[0]] == "."\
                    and s[y + dy[1]][x + dx[1]] == "."\
                    and s[y + dy[2]][x + dx[2]] == "."\
                    and s[y + dy[3]][x + dx[3]] == ".":
                print("No")
                return
    print("Yes")
    return


main()

Submission status: Accepted

Solution code:
def main():
    # 下、右、上、左
    dy = [1, 0, -1, 0]
    dx = [0, 1, 0, -1]
    H, W = map(int, input().split())
    s = [["."] * (W + 2)]
    s.extend([["."] + list(input()) + ["."] for i in range(H)])
    s.append(["."] * (W + 2))
    for y in range(1, H + 1):
        for x in range(1, W + 1):
            if s[y][x] == "#" and s[y + dy[0]][x + dx[0]] == "."\
                    and s[y + dy[1]][x + dx[1]] == "."\
                    and s[y + dy[2]][x + dx[2]] == "."\
                    and s[y + dy[3]][x + dx[3]] == ".":
                print("No")
                return
    print("Yes")
    return


main()

Submission status: Accepted

Solution code:
def main():
    # 下、右、上、左
    dy = [1, 0, -1, 0]
    dx = [0, 1, 0, -1]
    H, W = map(int, input().split())
    s = [input() for i in range(H)]
    for y in range(H):
        for x in range(W):
            if s[y][x] == '.':
                continue
            cnt = 0
            for i, j in zip(dy, dx):
                ny = y + i
                nx = x + j
                if ny < 0 or H <= ny\
                        or nx < 0 or W <= nx\
                        or s[ny][nx] == '.':
                    continue
                cnt += 1
            if cnt == 0:
                print("No")
                return
    print("Yes")
    return


main()

Submission status: Accepted

Solution code:
# 下、右、上、左
dy = [1, 0, -1, 0]
dx = [0, 1, 0, -1]

H, W = map(int, input().split())
s = [input() for i in range(H)]


def main():
    for y in range(H):
        for x in range(W):

            if s[y][x] == '.':
                continue

            cnt = 0

            for i, j in zip(dy, dx):
                ny = y + i
                nx = x + j

                if ny < 0 or H <= ny\
                        or nx < 0 or W <= nx\
                        or s[ny][nx] == '.':
                    continue

                cnt += 1

            if cnt == 0:
                print("No")
                return

    print("Yes")
    return


main()

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))

ans = 'Yes'
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]

for i in range(1, h+1):
   for j in range(1, w+1):
      if (s[i][j] == '#'):
         ok = False
         for k in range(4):
            if (s[i+dx[k]][j+dy[k]] == '#'):
               ok = True
         if not ok:
            ans = 'No'

print(ans)

Submission status: Accepted

Solution code:
def paint(s, i, j):
    check = 0
    for jj in range(-1,2,2):
        if s[i][j+jj] == "#":
            check += 1
    for ii in range(-1,2,2):
        if s[i+ii][j] == "#":
            check += 1

    return check

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

for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == "#":
            if paint(s, i, j) == 0:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


def main():
    H, W = [int(x) for x in input().split()]

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

    s.append(['.' for i in range(W + 2)])

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

    print("Yes")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
Y,X=map(int,input().split())
info=[]
ans="Yes"
def search(y,x):
    c=0
    if not(0<=y<Y and 0<=x<X):
        return c
    if info[y][x]=="#":
        c=1
    return c
for i in range(Y):
    info.append(list(input()))
for i in range(Y):
    for j in range(X):
        if info[i][j]==".":
            continue
        if (search(i+1,j)+search(i-1,j)+search(i,j+1)+search(i,j-1))==0: #　#の上下左右に#がなかった。
            ans="No"
            break
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys

# 入力
H, W = map(int, input().split())

S = []
for i in range(H):
    S.append(input())

# 上下左右の座標
dx = [0,  -1, 1,  0]
dy = [1,  0, 0,  -1]

for i in range(H):
    for j in range(W):
        # ドットのマスはスキップ
        if S[i][j] == ".":
            continue

        # 塗りつぶせるかどうかのフラグ
        # 塗れるか否かはセルの上下左右のどこかに黒色のセルがいるかどうかで判断する
        can = False

        for k in range(4):
            ni = i + dx[k]
            nj = j + dy[k]

            if ni < 0 or H <= ni:
                continue
            if nj < 0 or W <= nj:
                continue

            if S[ni][nj] == "#":
                can = True
                break

        # 一つでも塗れないセルがあればNo
        if not can:
            print("No")
            sys.exit()

print("Yes")

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = [input() for _ in range(h)]
f = True
for i in range(h):
  for j in range(w):
    if s[i][j] == "#":
      l = [(i+1,j),(i-1,j),(i,j+1),(i,j-1)]
      t = []
      g = False
      for p,q in l:
        if p >= 0 and p < h and q >= 0 and q < w:
          if s[p][q] == "#":
            g = True
      if g is False:
        f = False
        break
  if f is False:
    break
if f:
  print("Yes")
else:
  print("No")

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        tmp = 0
        if s[i][j] == '#':
            try:
                if s[i-1][j] == '#':
                    tmp += 1
            except IndexError:
                pass
            try:
                if s[i+1][j] == '#':
                    tmp += 1
            except IndexError:
                pass
            try:
                if s[i][j-1] == '#':
                    tmp += 1
            except IndexError:
                pass
            try:
                if s[i][j+1] == '#':
                    tmp += 1
            except IndexError:
                pass
            if tmp == 0:
                flag = 1

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

Submission status: Accepted

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

s = [['.'] * (w+2)]
for i in range(h):
  tmp = list(input())
  tmp.insert(0, '.')
  tmp.append('.')
  s.append(tmp)

ans = 'Yes'
for i in range(1, h):
  for j in range(1, w):
    if s[i][j] == '#' and s[i][j+1] != '#' and s[i+1][j] != '#' and s[i][j-1] != '#' and s[i-1][j] != '#':
      ans = 'No'

print(ans)

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[list(input()) for i in range(H)]
flag=True
for i in range(H):
  for j in range(W):
    if S[i][j]==".":
      continue
    adj=[]
    if 0<i:
      adj.append(S[i-1][j])
    if i<H-1:
      adj.append(S[i+1][j])
    if 0<j:
      adj.append(S[i][j-1])
    if j<W-1:
      adj.append(S[i][j+1])
    # print(adj)
    if all('.'==m for m in adj):
      flag=False
print('Yes' if flag else 'No')

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [[""]*(w+2)]
for _ in range(h):
    _s = list(input())
    _s.insert(0, "")
    _s.append("")
    s.append(_s)
s.append([""]*(w+2))

dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]

c = [["."]*(w+2) for _ in range(h+2)]

for i in range(1, h+1):
    for j in range(1, w+1):
        if s[i][j] != "#":
            continue

        for _dx, _dy in zip(dx, dy):
            x = i + _dx
            y = j + _dy
            if s[x][y] == "#":
                c[i][j] = "#"
                c[x][y] = "#"

ans = "Yes"
for i in range(1, h+1):
    for j in range(1, w+1):
        if s[i][j] != c[i][j]:
            ans = "No"
            break

print(ans)

Submission status: Accepted

Solution code:
H,W=map(int, input().split())
S = [input() for _ in range(H)]
d = [-1,0,1,0,-1]
b = 0
for h in range(H):
    for w in range(W):
        if S[h][w] == ".": continue
        if any(S[h+x][w+y]=="#" for x,y in zip(d,d[1:]) if 0<=h+x<H and 0<=w+y<W):
            continue
        b = 1
print("YNeos"[b::2])

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
H,W=map(int,input().split())
L=[]
for i in range(H):
    L.append([i for i in input()])
flg=0
cnt=0
#print(L)
for i in range(H):
    for j in range(1,W-1):
        if L[i][j]=='#' and L[i][j-1]!='#' and L[i][j+1]!='#':
            if L[i-1][j]!='#' and L[i+1][j]!='#':
                print('No')
                flg=1
                break
    cnt+=1
    if flg==1:
        break

#print(cnt)
if cnt==H:
    print('Yes')

Submission status: Accepted

Solution code:
h, w = [int(i) for i in input().split(' ')]
tg = [input() for i in range(h)]
og = [['.']*w]*h

fail = False
for i in range(h):
    for j in range(w):
        # Blank
        if tg[i][j] == ".":
            continue

        # up direction
        if i > 0 and tg[i-1][j] == '#':
            og[i][j] = '#'
            og[i-1][j] = '#'
            continue

        # down direction
        if i < h-1 and tg[i+1][j] == '#':
            og[i][j] = '#'
            og[i+1][j] = '#'
            continue

        # left direction
        if j > 0 and tg[i][j-1] == '#':
            og[i][j] = '#'
            og[i][j-1] = '#'
            continue

        # right direction
        if j < w-1 and tg[i][j+1] == '#':
            og[i][j] = '#'
            og[i][j+1] = '#'
            continue

        fail = True
        break

    if fail:
        print("No")
        break

if not fail:
    print("Yes")

Submission status: Accepted

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

def is_move(i,j):
    if j < 0 or j > w - 1 or i < 0 or i > h - 1:
        return False
    return True

di=[0,1,0,-1]
dj=[1,0,-1,0]
flag = False
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            flag_4 = False
            for k in range(4):
                I = i + di[k]
                J = j + dj[k]
                if is_move(I, J):
                    if s[I][J] == "#":
                        flag_4 = True
            if not flag_4:
                flag = True

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

Submission status: Accepted

Solution code:
# coding: utf-8
import sys

sys.setrecursionlimit(200000)

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

canvas=[]

flg=True

def bfs(y,x):
    cnt[0]+=1
    canvas[y][x]="."
    dx=[1,0,-1,0]
    dy=[0,1,0,-1]
    for i in range(4):
        ny=y+dy[i]
        nx=x+dx[i]
        
        if 0<=nx<=W-1 and 0<=ny<=H-1:
            if canvas[ny][nx]=="#":
                bfs(ny,nx)

for i in range(H):
    row=list(input())
    canvas.append(row)


for i in range(H):
    for j in range(W):
        if canvas[i][j]=="#":
            cnt=[0]
            bfs(i,j)
            if cnt[0]==1:
                flg=False

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

Submission status: Accepted

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


a="Yes"

for i in range(h):
    if a=="No":
        break
    for j in range(w):
        a="No"
        if s[i][j]==".":
            a="Yes"
        else:
            if i-1>=0 and s[i-1][j]=="#":
                a="Yes"
            elif j-1>=0 and s[i][j-1]=="#":
                a="Yes"
            elif j+1<w and s[i][j+1]=="#":
                a="Yes"
            elif i+1<h and s[i+1][j]=="#":
                a="Yes"
        if a=="No":
            break


print(a)

Submission status: Accepted

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

for i in range(h):
    s[i].insert(0, ".")
    s[i].append(".")

s1 = []
for i in range(w+2):
    s1.append(".")

s.insert(0, s1)
s.append(s1)

ans = "Yes"

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][j-1] == s[i][j+1] == s[i+1][j] == ".":
                ans = "No"

print(ans)

Submission status: Accepted

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

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

dxy = [(0, -1), (1, 0), (0, 1), (-1, 0)]
ans = 'Yes'

for y in range(H):
  for x in range(W):
    if li[y][x] == '.':
      continue

    a = 0
    for dx, dy in dxy:
      if x+dx < 0 or x+dx > W-1 or y+dy < 0 or y+dy > H-1:
        continue
      if li[y+dy][x+dx] == '#':
        a = 1
    
    if a == 0:
      ans = 'No'

print(ans)

Submission status: Accepted

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

#H x Wのマスを全て"."の行列で覆う （H+2 x W+2） 
S1 = [list(map(str,input())) for i in range(H)]
for i in range(H):
    S1[i].insert(0,'#')
    S1[i].append('#')
S = S = [['#'] * (W + 2)] + S1 + [['#'] * (W + 2)]

cnt = 0

for i in range(1,H):
    for j in range(1,W):
        if S[i][j] == '#':
            if S[i-1][j] == S[i][j-1] == S[i][j+1] == S[i+1][j] == '.':
                cnt += 1
if cnt == 0:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:

def GridRepainting():
    H, W = map(int, input().split())
    strlist = []
    for _ in range(0, H):
        strlist.append(list(input()))
    for i in range(0, H):
        for j in range(0, W):
            if strlist[i][j] == '#':
                if i > 0:
                    if strlist[i - 1][j] == '#':continue
                if i != H - 1:
                    if strlist[i + 1][j] == '#':continue
                if j > 0:
                    if strlist[i][j - 1] == '#':continue
                if j != W - 1:
                    if strlist[i][j + 1] == '#':continue
                print('No')
                return()

    print('Yes')
    return()

GridRepainting()

Submission status: Accepted

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

# 番兵
grid = [["."] + list(input()) + ["."] for _ in range(H)]
grid = ["."*(W+2)] + grid + ["."*(W+2)] # 上下に番兵

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

Submission status: Accepted

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

s = []


for i in range(H):
    s.append(input())



for i in range(H):
    for j in range(W):
        if s[i][j] == '#':

            l, u, r, d = 0, 0, 0, 0

            if j > 0 and s[i][j-1] == '#':
                l = 1

            if i > 0 and s[i-1][j] == '#':
                r = 1

            if j < W-1 and s[i][j+1] == '#':
                u = 1

            if i < H-1 and s[i+1][j] == '#':
                d = 1


            if l+u+r+d == 0:
                print('No')
                exit()

print('Yes')

Submission status: Accepted

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

bre = False
for h in range(H):
    for w in range(W):
        if s[h][w] == "#":
            if h>0 and s[h-1][w]=="#":
                continue
            elif h<H-1 and s[h+1][w]=="#":
                continue
            elif w>0 and s[h][w-1]=="#":
                continue
            elif w<W-1 and s[h][w+1]=="#":
                continue
            else:
                #print(h,w)
                print("No")
                bre = True
                break
    if bre:
        break
else:
    print("Yes")

Submission status: Accepted

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

flg = True
for i in range(h):
    for j in range(w):
        if mat[i][j] == "#":
            check = False
            if i != 0 and mat[i-1][j] == "#":
                check = True
            elif i != h-1 and mat[i+1][j] == "#":
                check = True
            elif j != 0 and mat[i][j-1] == "#":
                check = True
            elif j != w-1 and mat[i][j+1] == "#":
                check = True

            if not check:
                flg = False
                break

if flg:
    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)]
#print(*s, sep="\n")
for i in range(1,H+1):
    for j in range(1,W+1):
        if s[i][j] == "#":
            if s[i-1][j]=="." and s[i][j-1]=="." and s[i+1][j]=="." and s[i][j+1]==".":
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
HW = input().split()
H, W = int(HW[0]), int(HW[1])
S = [input() for _ in range(H)] 
result = [[0 for _ in range(W)] for _ in range(H)]

ans = 'Yes'
for i in range(H):
  for j in range(W):
    if S[i][j] == '.':
      continue
    else:
      if (i-1 >= 0 and j-1 >= 0 and S[i-1][j] == '.') and (j-1 >= 0 and S[i][j-1] == '.') and ( j+1 < W and S[i][j+1] == '.') and (i+1 < H and S[i+1][j] == '.'):
        ans = 'No'
print(ans)

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        if lst[i][j] == "#":
            if (
                (j > 0 and lst[i][j - 1] != "#")
                and (j < W - 2 and lst[i][j + 1] != "#")
                and (i > 0 and lst[i - 1][j] != "#")
                and (i < H - 2 and lst[i + 1][j] != "#")
            ):
                print("No")
                exit()
print("Yes")

Submission status: Accepted

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

for i in range(h):
    ls[i] = ["."] + ls[i] + ["."]

ls_s = [["."] * len(ls[0])]
ls_s.extend(ls)
ls_s.extend([["."] * len(ls[0])])

for i in range(1, h+1):
    for j in range(1, w+1):
        if ls_s[i][j] == "#":
            if ls_s[i-1][j] == ls_s[i+1][j] == ls_s[i][j-1] == ls_s[i][j+1] == ".":
                print("No")
                break
            elif i == h and j == w:
                print("Yes")
        elif i == h and j == w:
            print("Yes")
    else:
        continue
    break

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
wb = {'#':1, '.':0}
wall = 0
edges = [wall for i in range(w+2) ]
# grid = [list(map(wb.get, input().split())) for _ in range(h)]
grid = [edges]
for i in range(1,h+1):
    S = [wall]+list(map(wb.get, input()))+[wall]
    grid.append(S)
grid.append(edges)
# [print(i) for i in grid]
dxy = [(1,0), (-1,0), (0,1), (0,-1)]
for y in range(1,h+1):
    for x in range(1,w+1):
        if grid[y][x] == 0:
            continue
        for dy, dx in dxy:
            y_,x_ = y+dy,x+dx
            # print(y_, x_)
            if grid[y_][x_]:
                break
        else:
            # print(y,x, grid[y][x])
            print('No')
            exit()
print('Yes')

Submission status: Accepted

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

for h in range(H):
    for w in range(W):
        if s[h][w] == "#":
            if all([s[h + dh][w + dw] == "." for dh, dw in d]):
                print("No")
                sys.exit()
print("Yes")

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
x = ["." for i in range(w+2)]
z = [x]
for i in range(h):
    t = ["."]
    y = list(input())
    t = t + y + t
    z.append(t)
z.append(x)
ans = 0
for i in range(1,w+1):
    for j in range(1,h+1):
        if z[j][i] == "#":
            if (z[j+1][i] ==
                z[j-1][i] ==
                z[j][i+1] ==
                z[j][i-1] == "."):
                ans = 1
if ans == 0:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
s=[list(input()) for _ in range(H)]
cnt=0
for i in range(H):
    for j in range(W):
        if s[i][j]=='#':
            l=[[i-1,j],[i+1,j],[i,j-1],[i,j+1]]
            OK=0
            for v in l:
                if H>v[0]>=0 and W>v[1]>=0:
                    if s[v[0]][v[1]]=='#':
                        OK=1
            if OK==0:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
H,W=map(int,input().split())
s=[]
s0=""
for i in range(W+2):
    s0+="."
s.append(s0)
for i in range(H):
    ss="."
    ss+=input().strip()+"."
    s.append(ss)
s.append(s0)
#print(s)
flg=True
for h in range(H+1):
    for w in range(W+1):
        if s[h][w]=="#":
            flg=False
            for i in [1,-1]:
                if s[h+i][w]=="#":
                    flg=True
                if s[h][w+i]=="#":
                    flg=True
            if flg==False:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

Solution code:
import sys

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

rows = []
for i in range(H):
    chars = list(input())
    rows.append([1 if c == '#' else 0 for c in chars])


def is_possible(rows):
    for h in range(H):
        for w in range(W):
            if rows[h][w] == 0:
                continue

            pair_candidates = []
            if w - 1 >= 0:
                pair_candidates.append((h, w - 1))
            if w + 1 < W:
                pair_candidates.append((h, w + 1))
            if h - 1 >= 0:
                pair_candidates.append((h - 1, w))
            if h + 1 < H:
                pair_candidates.append((h + 1, w))
            pair_candidates_sum = sum([
                rows[_h][_w] for _h, _w in pair_candidates
            ])

            if pair_candidates_sum == 0:
                return False

    return True


if is_possible(rows):
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
H, W = map(int,input().split())
A=[list(input()) for _ in range(H)]
def cnt_(h,w):
  ret=0
  if 0<=h<H and 0<=w<W and A[h][w]=='#':
    ret=1
  return ret
def cnt(h,w):
  ret=0
  ret+=cnt_(h+1,w)+cnt_(h-1,w)
  ret+=cnt_(h,w+1)+cnt_(h,w-1)
  return ret
ans = "Yes"
for i in range(H):
  for j in range(W):
    if A[i][j]=='#':
      if cnt(i,j)==0:
        ans = "No"
        break
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
h,w = map(int,input().split())
lst1 = [list(list(input())) for i in range(h)]
    
#たかだか2500マスで、上下左右のショートカットをしてもあまり意味はないので4近傍全探索

def dfs(maze,sh, sw):
    for j, k in ([1, 0], [-1, 0], [0, 1], [0, -1]): #4近傍について探索
        new_h, new_w = sh+j, sw+k
        if new_h < 0 or new_h >= h or \
            new_w < 0 or new_w >= w: #リスト外でないならelif条件へ
            continue
        elif maze[new_h][new_w] == "#":
            return True
    return False

for i in range(h):
    for j in range(w):
        if lst1[i][j] == "#":
            if not dfs(lst1,i,j):
                print("No")
                exit()
print("Yes")