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:
h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
for i in range(h - 1):
    for j in range(w - 1):
        if s[i][j] == "#" and 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:
h,w=map(int,input().split())
s=[['カス'for i in range(w+2)]]
for i in range(h):
    s.append(['カス']+list(input())+['カス'])
s.append(['カス'for i in range(w+2)])
p=0
pp=0
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j]=='#':
            p+=1
            if s[i+1][j]=='#' or s[i-1][j]=='#' or s[i][j-1]=='#' or s[i][j+1]=='#':
                pp+=1
print('Yes' if p==pp else 'No')

Submission status: Accepted

Solution code:
import sys
# input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

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

G = [['.'] * ((W + 2))]
for _ in range(H):
    tmp = ['.'] + list(input()) + ['.']
    G.append(tmp)

G.append(['.'] * (W + 2))

move = [[1, 0], [-1, 0], [0, 1], [0, -1]]

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if G[i][j] == '#':
            flag = False
            for dx, dy in move:
                if G[i + dx][j + dy] == '#':
                    flag = True
                    break
            if not flag:
                print ('No')
                exit()

print ('Yes')

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    H, W = map(int, input().split())
    S = [list(input()) for _ in range(H)]

    for h in range(H):
        for w in range(W):
            if S[h][w] == "#":
                for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1)):
                    next_h, next_w = h + dh, w + dw
                    if next_h < 0 or next_h >= H or next_w < 0 or next_w >= W:
                        continue
                    elif S[next_h][next_w] == "#":
                        break
                else:
                    print("No")
                    exit()

    print("Yes")


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
b=[list("."*(w+2))]
for i in range(h):
  b.append(list("."+input()+"."))
b.append(list("."*(w+2)))
ans="Yes"
dx=[0,1,0,-1]
dy=[1,0,-1,0]
for i in range(1,h+1):
  for j in range(1,w+1):
    if b[i][j]==".":continue
    flag=True
    for x,y in zip(dx,dy):
      if b[i][j]==b[i+x][j+y]:flag=False
    if flag:ans="No"
print(ans)

Submission status: Accepted

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

for i in range(h):
  for j in range(w):
    if s[i][j]=='#':
      check=0
      for x,y in [1,0],[-1,0],[0,1],[0,-1]:
        ny=i+y
        nx=j+x
        if 0<=ny<=h-1 and 0<=nx<=w-1:
          if s[ny][nx]=='#':
            check=1
      if check==0:
        print('No')
        exit()
print('Yes')

Submission status: Accepted

Solution code:
def main():
    H, W = (int(i) for i in input().split())
    A = [input() for i in range(H)]
    dhw = ((0, -1), (0, 1), (-1, 0), (1, 0))
    for h in range(H):
        for w in range(W):
            if A[h][w] == "#":
                for dh, dw in dhw:
                    if h + dh < 0 or H <= h + dh or \
                            w + dw < 0 or W <= w + dw:
                        continue
                    if A[h+dh][w+dw] == "#":
                        break
                else:
                    return print("No")
    print("Yes")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [input() for i in range(H)]
for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            if i > 0 and s[i-1][j] == '#':
                continue
            elif i < H-1 and s[i+1][j] == '#':
                continue
            elif j > 0 and s[i][j-1] == '#':
                continue
            elif j < W-1 and s[i][j+1] == '#':
                continue
            else:
                print("No")
                exit(0)
print("Yes")

Submission status: Accepted

Solution code:
def check():
    H, W = map(int, input().split())
    S = [0]*(H+2)
    S[0] = '.'*(W+2)
    S[H+1] = '.'*(W+2)
    dh = [0,1,0,-1]
    dw = [1,0,-1,0]
    for h in range(1,H+1):
        S[h] = '.' + input() + '.'
    for h in range(H):
        for w in range(W):
            if S[h][w]=='#':
                for i in range(4):
                    if S[h+dh[i]][w+dw[i]]=='#':
                        break
                else:
                    return 'No'
    return 'Yes'
print(check())

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if board[i][j] == '.': continue
        ok = False
        for k in (-1, 0, 1):
            for l in (-1, 0, 1):
                if k == 0 and l == 0:
                    continue
                if k != 0 and l != 0:
                    continue
                # print(k, l)
                ni = i + k
                nj = j + l
                if ni < 0 or ni >= h or nj < 0 or nj >= w: continue
                # if i == 1 and j == 1:
                if board[ni][nj] == '#':
                    ok |= True
        if not ok:
            print('No')
            exit(0)

print('Yes')

Submission status: Accepted

Solution code:
def main():
    h, w = map(int, input().split())
    s = [["" for _ in range(w + 2)]]
    for _ in range(h):
        s.append([""] + list(input()) + [""])
    s.append(s[0])
    f = False
    for i in range(1, h + 1):
        if f:
            break
        for j in range(1, w + 1):
            if s[i][j] == "#":
                if not any(s[i + di][j + dj] == "#" for di, dj in [[0, 1], [1, 0], [0, -1], [-1, 0]]):
                    print("No")
                    f = True
                    break
    if not f:
        print("Yes")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

H, W = map(int, input().split())
S = [list(sys.stdin.readline().strip()) for _ in range(H)]

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

if len(S) == len(S[0]) == 1:
    if S[0][0] == "#":
        print("Yes")
    else:
        print("No")
    exit()

for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            ok = False
            for k in range(4):
                ny = i + dy[k]
                nx = j + dx[k]
                if not (0 <= ny <= H - 1 and 0 <= nx <= W - 1):
                    continue
                if S[ny][nx] == "#":
                    ok = True
                    break
            if ok is False:
                print("No")
                exit()
print("Yes")

Submission status: Accepted

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

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


for i in range(H):
    for j in range(W):
        if M[i][j] == '#':
            flag = False
            for k in range(4):
                nx = j + dy[k]
                ny = i + dx[k]
                if (nx < 0) or (ny < 0) or (nx >= W) or (ny >= H):
                    continue
                elif M[ny][nx] == '#':
                    flag = True
                    break
            if not flag:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest


def resolve():
    h, w = map(int, input().split())
    sw = [list(input()) for _ in range(h)]

    for i in range(h):
        for j in range(w):
            np_sharp = False
            if sw[i][j] == '#':
                for add_i, add_j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
                    next_i = i + add_i
                    next_j = j + add_j

                    if next_i < 0 or next_j < 0 or h <= next_i or w <= next_j:
                        continue
                    else:
                        # 1つでも#があればOK
                        # すべて.だとNG
                        if sw[next_i][next_j] == '#':
                            np_sharp = True

                if not np_sharp:
                    print('No')
                    return
            else:
                continue

    print('Yes')



class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3 3
.#.
###
.#."""
        output = """Yes"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """5 5
#.#.#
.#.#.
#.#.#
.#.#.
#.#.#"""
        output = """No"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """11 11
...#####...
.##.....##.
#..##.##..#
#..##.##..#
#.........#
#...###...#
.#########.
.#.#.#.#.#.
##.#.#.#.##
..##.#.##..
.##..#..##."""
        output = """Yes"""
        self.assertIO(input, output)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
import itertools
h, w = map(int, input().split())
s = [input() for i in range(h)]
for i, j in itertools.product(range(h), range(w)):
  if s[i][j] == '#' and (i == 0 or s[i - 1][j] == '.') and (j == 0 or s[i][j - 1] == '.') and (i == h - 1 or s[i + 1][j] == '.') and (j == w - 1 or s[i][j + 1] == '.'):
    print('No')
    break
else:
  print('Yes')

Submission status: Accepted

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

ans = 'Yes'
for i in range(H):
    for j in range(W):
        if G[i][j] == '#':
            if i == 0:
                if j == 0:
                    if G[i+1][j] == '#' or G[i][j+1] == '#':
                        continue
                    else:
                        ans = 'No'
                        break
                elif j == W-1:
                    if G[i+1][j] == '#' or G[i][j-1] == '#':
                        continue
                    else:
                        ans = 'No'
                        break
                else:
                    if G[i+1][j] == '#' or G[i-1][j] == '#' or G[i][j-1] == '#' or G[i][j+1] == '#':
                        continue
                    else:
                        ans = 'No'
                        break
            elif i == H-1:
                if j == 0:
                    if G[i-1][j] == '#' or G[i][j+1] == '#':
                        continue
                    else:
                        ans = 'No'
                        break
                elif j == W-1:
                    if G[i][j-1] == '#' or G[i-1][j]:
                        continue
                    else:
                        ans = 'No'
                        break
                else:
                    if G[i-1][j] == '#' or G[i][j-1] == '#' or G[i][j+1] == '#':
                        continue
                    else:
                        ans = 'No'
                        break
            else:
                if j == 0:
                    if G[i+1][j] == '#' or G[i][j+1] == '#' or G[i-1][j]:
                                continue
                    else:
                        ans = 'No'
                        break
                elif j == W-1:
                    if G[i+1][j] == '#' or G[i][j-1] == '#' or G[i-1][j]:
                        continue
                    else:
                        ans = 'No'
                        break
                else:
                    if G[i+1][j] == '#' or G[i-1][j] == '#' or G[i][j-1] == '#' or G[i][j+1] == '#':
                        continue
                    else:
                        ans = 'No'
                        break
    if ans == 'No':
        break

print(ans)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
grid = [input() for _ in range(H)]
UDLR = [[-1, 0], [1, 0], [0, -1], [0, 1]]

for i in range(H):
    for j in range(W):
        here = grid[i][j]
        if here == '.':
            continue
        ng = True
        for direction in UDLR:
            side_h = i + direction[0]
            side_w = j + direction[1]
            if 0 <= side_h < H and 0 <= side_w < W:
                side = grid[side_h][side_w]
                if side == '#':
                    ng = False
        if ng:
            print('No')
            exit()

print('Yes')

Submission status: Accepted

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

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

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

        if S[y][x] == ".":
            continue

        cnt = 0

        for k in range(4):
            ny = y + dy[k]
            nx = x + dx[k]

            if ny < 0 or ny >= H or nx < 0 or nx >= W:
                continue
            
            flg = False

            if S[ny][nx] == "#":
                flg = True
                break
        if flg:
            continue
        else:
            print("No")
            exit()

print("Yes")

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
a=[0]*(w+2)
m=[a]
ans='Yes'
for i in range(h):
    s=input()
    m.append([0]+[1 if x=='#' else 0 for x in s]+[0])
m.append(a)
for i in range(1,h+1):
    for j in range(1,w+1):
        if m[i][j]==1 and sum(m[i][j-1:j+2],m[i-1][j]+m[i+1][j])==1:
            ans='No'
print(ans)

Submission status: Accepted

Solution code:
import sys
H, W = map(int, input().split())
s = []
s.append('.' * (W + 2))
for i in range(1, H + 1):
    a = input()
    a = '.' + a + '.'
    s.append(a)
s.append('.' * (W + 2))
for i in range(1, H + 1):
    for j in range(1, W + 1):
        if s[i][j] == '#':
            OK = False
            if s[i - 1][j] == '#' or s[i + 1][j] == '#' or s[i][j - 1] == '#' or s[i][j + 1] == '#':
                OK = True
            if OK == False:
                print('No')
                sys.exit()
print('Yes')

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
dx = [-1, 0, 1]
dy = [-1, 0, 1]

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            flag = 0
            for d_x in dx:
                for d_y in dy:
                    if (d_x == 0 and d_y == 0) or (d_x == -1 and d_y == -1) or (d_x == 1 and d_y == 1)\
                            or (d_x == 1 and d_y == -1) or (d_x == -1 and d_y == 1):
                        continue
                    if not (d_x == 0 and d_y == 0) and 0 <= i + d_x < h and 0 <= j + d_y < w \
                            and s[i + d_x][j + d_y] == '#':
                        flag = 1

            if flag == 0:
                print('No')
                exit(0)

print('Yes')

Submission status: Accepted

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

matrix = []
ans = "Yes"

for _ in range(H):
    siw = list(input())
    matrix.append(siw)

memo = []

for h in range(H):
    for w in range(W):
        if matrix[h][w] == "#":
            if w == 0:
                if matrix[h][w+1] == ".":
                    memo.append((h,w))
            elif w == W-1:
                if matrix[h][w-1] == ".":
                    memo.append((h,w))
            else:
                if matrix[h][w-1] == "." and matrix[h][w+1] == ".":
                    memo.append((h,w))

for h, w in memo:
    if h == 0:
        if matrix[h+1][w] == ".":
            ans = "No"
            break
    elif h == H-1:
        if matrix[h-1][w] == ".":
            ans = "No"
            break
    else:
        if matrix[h-1][w] == "." and matrix[h+1][w] == ".":
            ans = "No"
            break

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(200000)


def read():
    H, W = map(int, input().strip().split())
    S = []
    for i in range(H):
        s = list(input().strip())
        S.append(s)
    return H, W, S


def solve(H, W, S):
    if H == 1 and  W == 1 and S[0][0] == "#":
        return "No"
    
    dd = [(1, 0), (0, 1), (-1, 0), (0, -1)]

    def dfs(y, x):
        depth = 1
        S[y][x] = "."
        for dy, dx in dd:
            ny = y + dy
            nx = x + dx
            if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == "#":
                depth = max(depth, dfs(ny, nx) + 1)
        return depth

    for i in range(H):
        for j in range(W):
            if S[i][j] == "#":
                depth = dfs(i, j)
                if depth == 1:
                    return "No"
    return "Yes"


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Submission status: Accepted

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

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

ans="Yes"
for y in range(0, H):
    for x in range(0, W):
        if s[y][x]==".":
            continue

        c=0
        for d in range(-1, 2, 2):
            xx = x + d
            if 0<=xx and xx<W:
                if s[y][xx]=="#":
                    c+=1
            yy = y + d
            if 0<=yy and yy<H:
                if s[yy][x]=="#":
                    c+=1
        if c==0:
            ans="No"
print(ans)

Submission status: Accepted

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

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

ans="Yes"
for y in range(0, H):
    for x in range(0, W):
        if s[y][x]==".":
            continue

        c=0
        for dx in range(-1, 2, 2):
            xx = x + dx
            if 0<=xx and xx<W:
                if s[y][xx]=="#":
                    c+=1
                    
        for dy in range(-1, 2, 2):
            yy = y + dy
            if 0<=yy and yy<H:
                if s[yy][x]=="#":
                    c+=1

        if c==0:
            ans="No"
print(ans)

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[list(input()) for a in range(h)]
check=[[-1,0],[0,-1],[1,0],[0,1]]
for i in range(h):
    for j in range(w):
        cnt=0
        if s[i][j]=="#":
           if i>=1:
                if s[i+check[0][0]][j+check[0][1]]=="#":
                    cnt+=1
           if j>=1:
                if s[i+check[1][0]][j+check[1][1]]=="#":
                    cnt+=1
           if i <= h-2:
               if s[i + check[2][0]][j + check[2][1]]=="#":
                    cnt+=1
           if j <= w - 2:
               if s[i + check[3][0]][j + check[3][1]]=="#":
                   cnt+=1
           if cnt == 0:
               print("No")
               exit()

print("Yes")

Submission status: Accepted

Solution code:
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] == "#":
      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:
#!/usr/bin/env python
# coding: utf-8

# In[2]:


# H, W = map(int, input().split())
# data = [["a"]*(W+2)]
# for i in range(H):
#     data.append(list("a" + input() + "a"))
# data.append([["a"]*(W+2)])
# # print(data)


# In[5]:


# for h in range(H+1):
# #     print(data[h])
#     for w in range(W+1):
# #         print(data[h][w])
#         if data[h][w] == "#":
#             if data[h-1][w] != "#" and data[h][w-1] != "#" and data[h][w+1] != "#" and data[h+1][w] != "#":
#                 print("No")
#                 exit()

# print("Yes")


# In[ ]:


h, w = map(int, input().split())
s = [["a"]*(w + 2)]
 
for i in range(h):
    s.append(list("a" + input() + "a"))
s.append(["a"]*(w + 2))
 
for i in range(h + 1):
    for j in range(w + 1):
        if s[i][j] == "#":
            if s[i - 1][j] != "#" and s[i][j - 1] != "#" and s[i][j + 1] != "#" and s[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(1,H-1):
    for j in range(1,W-1):
        if S[i][j]== "#" and S[i+1][j] == S[i][j+1] == S[i-1][j] == S[i][j-1] == ".":
            print("No")
            exit()
print("Yes")

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
C = [list(input()) for i in range(H)]
for i in range(H):
  for j in range(W):
    if C[i][j]=='#':
      if (i>0 and C[i-1][j]=='.') and (i<H-1 and C[i+1][j]=='.') and (j>0 and C[i][j-1]=='.') and (j<H-1 and C[i][j+1]=='.'):
        print("No")
        exit()
print("Yes")

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [["a"]*(w + 2)]

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

for i in range(h + 1):
    for j in range(w + 1):
        if s[i][j] == "#":
            if s[i - 1][j] != "#" and s[i][j - 1] != "#" and s[i][j + 1] != "#" and s[i + 1][j] != "#":
                print("No")
                exit()

print("Yes")

Submission status: Accepted

Solution code:
import sys

h,w=map(int,input().split())
C=[input() for i in range(h)]
for i in range(h):
    for j in range(w):
        if C[i][j]==".":
            continue
        flag=0
        if i!=0:
            if C[i-1][j]=="#":
                flag=1
        if i!=h-1:
            if C[i+1][j]=="#":
                flag=1
        if j!=0:
            if C[i][j-1]=="#":
                flag=1
        if j!=w-1:
            if C[i][j+1]=="#":
                flag=1
        if flag==0:
            print("No")
            sys.exit()
print("Yes")

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]

def main():
    H, W = map(int, readline().rstrip().split())
    S = [readline().rstrip() for _ in range(H)]
    res = 'Yes'
    for h in range(H):
        for w in range(W):
            if S[h][w] != '#':
                continue
            flg = False
            for dh, dw in directions:
                if 0 <= h+dh < H and 0 <= w+dw < W:
                    if S[h+dh][w+dw] == '#':
                        flg = True
                        break
            if not flg:
                res = 'No'
                break
        else:
            continue
        break

    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
h, w = map(int, input().split())
grid = []

for i in range(h):
    grid.append(list(input()))

for i in range(h):
    for j in range(w):
        if grid[i][j] == '#':
            # 左右のマスをチェック
            if 0 < j and grid[i][j-1] == '#':
                continue
            if j < (w - 1) and grid[i][j+1] == '#':
                continue
            # 上下のマスをチェック
            if 0 < i and grid[i-1][j] == '#':
                continue
            if i < (h - 1) and grid[i+1][j] == '#':
                continue
            # 接するマスに#がなかった場合は終了
            print("No")
            exit()

print("Yes")

Submission status: Accepted

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


map_input = [list(map(str,list(input()))) for i in range(h)]

map_list = [['.']*(w+2)]
for m in map_input:
    map_list.append(['.']+m+['.'])
map_list.append(['.']*(w+2))

for i in range(h+2):
    for j in range(w+2):
        if map_list[i][j]=='#':
            if map_list[i+1][j]=='#':
                continue
            elif map_list[i-1][j]=='#':
                continue
            elif map_list[i][j+1]=='#':
                continue
            elif map_list[i][j-1]=='#':
                continue
            else:
                print('No');exit()

print('Yes')

Submission status: Accepted

Solution code:
import itertools

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

ok = True        
for i in range(H):
  for j in range(W):
    if S[i][j] == '#':
      flg = False
      for x,y in itertools.product([-1,0,1],repeat=2):
        if not (abs(x) + abs(y) == 1):
          continue
        nx = i + x;ny = j + y
        if 0 <= nx < H and 0 <= ny < W:
          if S[nx][ny] == '#':
            flg = True
      if not flg:
        ok = False
        break
if ok:
  print('Yes')
else:
  print('No')

Submission status: Accepted

Solution code:
H, W = map(int,input().split())
List = [list("." +input()+ ".") for i in range(H)]
B = ["."]*(W+2)
List.insert(0,B)
List.append(B)

for i in range(1,H+1):
  for j in range(1,W+1):
    if List[i][j]=="#" and List[i-1][j]==List[i][j-1]\
        ==List[i][j+1]==List[i+1][j]==".":
      print("No")
      break
  else:
    continue
  break  

else:print("Yes")

Submission status: Accepted

Solution code:


h, w = map(int,input().split())
# In[]
s = [list(input()) for _ in range(h)]
dx = [0,1,0,-1]
dy = [1,0,-1,0]

count = 0

for y in range(h):
    for x in range(w):
        cnt = 0
        for k in range(4):
            ny = y + dy[k]
            nx = x + dx[k]
            if nx < 0 or nx>=w or ny < 0 or ny >=h:
                continue
            if s[y][x] == '.':
                cnt +=1
                continue
            if s[y][x] == '#':
                if s[ny][nx] == '#':
                    cnt += 1
        if cnt == 0:
            print('No')
            exit()

                        
print('Yes')

Submission status: Accepted

Solution code:
#反転ではない
#孤立点の有無
h,w=map(int, input().split( ))
s= ["."*(w+2)]+["."+input()+"." for _ in range(h)]+["."*(w+2)]
od=0
ev=0
nb=[(1,0),(-1,0),(0,1),(0,-1)]
for i in range(1,h+1):
    for j in range(1,w+1):
        flag=False
        for x,y in nb:
            if s[i+x][j+y]=="#":
                flag=True
                break
        if not flag and s[i][j]=="#":
            print("No")
            exit()
        
        
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):
    for j in range(W):
        if s[i][j] == '#':
            count = 0
            for dx, dy in ((-1,0), (0,1), (1,0), (0,-1)):
                x, y = i+dx, j+dy
                if 0 <= x and 0 <= y and x <= H-1 and y <= W-1:
                    if s[x][y] == '.':
                        count += 1
            if count == 4:
                print('No')
                exit()

print('Yes')

Submission status: Accepted

Solution code:
def main():
    from collections import deque

    h, w = map(int, input().split())
    q = deque([])
    buffer = ["." for _ in range(w + 2)]
    for _ in range(h):
        r = ["."] + list(input()) + ["."]
        while q:
            i = q.pop()
            if r[i] == '.':
                print("No")
                exit()

        for i, (x, y, z) in enumerate(zip(r, r[1:], r[2:])):
            if (x, y, z) == (".", "#", ".") and buffer[i + 1] == ".":
                q.append(i + 1)

        buffer = r
    else:
        if q:
            print("No")
        else:
            print("Yes")


if __name__ == '__main__':
    main()

Submission status: Accepted

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

    m = []
    wall = ['.' for _ in range(w + 2)]
    m.append(wall)
    for _ in range(h):
        r = ["."] + list(input()) + ["."]
        m.append(r)
    m.append(wall)

    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if m[i][j] == '#':
                f = any([m[i + x][j + y] == "#" for (x, y) in ((1, 0), (-1, 0), (0, 1), (0, -1))])
                if not f:
                    print("No")
                    exit()
    else:
        print("Yes")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def gridChecker(grid):
    flag = True
    for r in range(H):
        for c in range(W):
            color = grid[r][c]
            if color == '#':
                if r == 0:
                    up = '_'
                else:
                    up = grid[r-1][c]
                if c == W-1:
                    right = '_'
                else :
                    right = grid[r][c+1]
                if r == H - 1:
                    down = '_'
                else :
                    down = grid[r+1][c]
                if c == 0:
                    left = '_'
                else :
                    left = grid[r][c-1]
                
                if '#' not in [up, right,down, left]:
                    flag = False
                    break
            
    return flag

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

grid = [[i for i in input()] for row in range(H)]

if gridChecker(grid):
    print('Yes')
else :
    print('No')

Submission status: Accepted

Solution code:
h, w=map(int, input().split())
table=[[0]*(w+2) for i in range(h+2)]
flag=0

for i in range(1, h+1):
    s=input()
    for j in range(1, w+1):
        table[i][j]=s[j-1]


for i in range(1, h+1):
    for j in range(1, w+1):
        if table[i][j]=="#":
            if table[i][j-1]!="#" and table[i-1][j]!="#" and table[i][j+1]!="#" and table[i+1][j]!="#":
                flag=1

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