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())
grid = [input() for _ in range(h)]

def check(i, j, h, w, grid):
    flg = False
    lst = [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]
    if i == 0:
        lst.remove((i-1, j))
    if j == 0:
        lst.remove((i, j-1))
    if i == h-1:
        lst.remove((i+1, j))
    if j == w-1:
        lst.remove((i, j+1))
    for tpl in lst:
        if grid[tpl[0]][tpl[1]] == '#':
            flg = True
    return flg

flg = True
for i in range(h):
    for j in range(w):
        if grid[i][j] == '#':
            if check(i, j, h, w, grid):
                pass
            else:
                flg = False
print('Yes') if flg else print('No')

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[list(input()) for _ in range(H)]
dist=[[0,1],[1,0],[-1,0],[0,-1]]
ans='Yes'

for h in range(H):
    for w in range(W):
        if S[h][w]=='#':
            for dh,dw in dist:
                if 0<=h+dh<=H-1 and 0<=w+dw<=W-1 and (S[h+dh][w+dw]=='#' or S[h+dh][w+dw]=='o'):
                    S[h+dh][w+dw]='o'
                    S[h][w]='o'

for s in S:
    if '#' in s:
        ans='No'
        
print(ans)

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
s=[]
s.append(['.']*(W+2))
#s=[list(map(str,input().split())) for i in range(H)]
for i in range(H):
    l=list(input())
    l.insert(0,'.')
    l.append('.')
    s.append(l)

s.append(['.']*(W+2))
flag=0
for i in range(H):
    for j in range(W):
        if s[i][j]=='#' and s[i][j-1]!='#' and s[i][j+1]!='#' and s[i-1][j]!='#' and s[i+1][j]!='#':
            flag=1
            break
if flag==1:
    print("No")
else:
    print("Yes")

Submission status: Accepted

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

hw = []
hw.append('.'*(W+2))
for _ in range(H):
    edited = input()
    edited = '.' + edited + '.'
    hw.append(edited)
hw.append('.'*(W+2))
#四方を白に囲まれた黒がある場合に達成不可能
for i in range(1,H+1):
    for j in range(1,W+1):
        if hw[i][j] == '#':
            #探索
            if hw[i+1][j] == '.' and hw[i-1][j] == '.' and hw[i][j+1] == '.' and hw[i][j-1] == '.':
                print('No')
                quit()

print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
l = []
l.append([input() for i in range(H)])
B = []
b = 0
m = 0
for i in range(H):
    for j in range(W):
        if l[0][i][j] == '#':
            B.append([i, j])
for i in B:
    for j in B:
        if i == j:
            pass
        elif i[0] == j[0] and i[1] - 1 <= j[1] <= i[1] + 1:
            m = 1
        elif i[0] - 1 <= j[0] <= i[0] + 1 and i[1] == j[1]:
            m = 1
        else:
            pass
    if m == 0:
        b = 1
        print('No')
        break
    else:
        m = 0
if b == 0:
    print('Yes')

Submission status: Accepted

Solution code:
H,W=(int(i) for i in 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 or s[i-1][j]=='.':
                if i==H-1 or s[i+1][j]=='.':
                    if j==0 or s[i][j-1]=='.':
                        if j==W-1 or s[i][j+1]=='.':
                            print('No')
                            exit()
print('Yes')

Submission status: Accepted

Solution code:
def solve(string):
    h, w, *s = string.split()
    h = int(h)
    w = int(w)
    for i in range(h):
        for j in range(w):
            if s[i][j] == ".":
                continue
            check = False
            if i > 0:
                check |= s[i][j] == s[i - 1][j]
            if i < h - 1:
                check |= s[i][j] == s[i + 1][j]
            if j > 0:
                check |= s[i][j] == s[i][j - 1]
            if j < w - 1:
                check |= s[i][j] == s[i][j + 1]
            if not check:
                return "No"
    return "Yes"


if __name__ == '__main__':
    n, m = map(int, input().split())
    print(solve('{} {}\n'.format(n, m) + '\n'.join([input() for _ in range(n)])))

Submission status: Accepted

Solution code:
def solve(string):
    h, w, *s = string.split()
    h = int(h)
    w = int(w)
    for i in range(h):
        for j in range(w):
            if s[i][j] == ".":
                continue
            check = False
            if i == 0 == h - 1:
                pass
            elif i == 0:
                check |= s[i][j] == s[i + 1][j]
            elif i == h - 1:
                check |= s[i][j] == s[i - 1][j]
            else:
                check |= s[i][j] == s[i + 1][j] or s[i][j] == s[i - 1][j]
            if j == 0 == w - 1:
                pass
            elif j == 0:
                check |= s[i][j] == s[i][j + 1]
            elif j == w - 1:
                check |= s[i][j] == s[i][j - 1]
            else:
                check |= s[i][j] == s[i][j + 1] or s[i][j] == s[i][j - 1]
            if not check:
                return "No"
    return "Yes"


if __name__ == '__main__':
    n, m = map(int, input().split())
    print(solve('{} {}\n'.format(n, m) + '\n'.join([input() for _ in range(n)])))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
mat = [input() for i in range(H)]
mat_t = [''.join(x) for x in zip(*mat)]
def check(mat, t):
    stack = set()
    for i, string in enumerate(mat):
        string = '.' + string + '.'
        for j in range(len(string)):
            if j+3<=len(string):
                if string[j:j+3] == '.#.':
                    if t==False:
                        stack.add((i, j))
                    else:
                        stack.add((j, i))
    return stack

stack1 = check(mat, False)
stack2 = check(mat_t, True)
if stack1 & stack2:
    print('No')
else:
    print('Yes')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [input() for i in range(H)]
flag = True
count = 0
loop_num = 0

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

for i in range(H):
    for j in range(W):
        if s[i][j] == '#':
            for k in range(4):
                if 0 <= i+dy[k] < H and 0 <= j+dx[k] < W:
                    #数えるマスの数
                    loop_num += 1
                    if s[i+dy[k]][j+dx[k]] == '.':
                        #白い隣あったマスの数
                        count += 1
            #隣り合うマスが存在しないとき黒いマスは塗りつぶせないので目標は達成不可
            if count == loop_num:
                flag = None
                break
            else:
                count = 0
                loop_num = 0
        else:
            pass
    if flag == None:
        break

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

Submission status: Accepted

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

mat = []
for i in range(h):
    mat.append(input())
def outrange(i,j,h,w):
    if i<0 or i>=h:
        return True
    if j<0 or j>=w:
        return True
    return False
for i in range(h):
    for j in range(w):
        if mat[i][j] == '.':
            continue
        #up
        if not outrange(i-1,j,h,w) and mat[i-1][j]=='#':
            continue
        if not outrange(i,j-1,h,w) and mat[i][j-1]=='#':
            continue
        if not outrange(i+1,j,h,w) and mat[i+1][j]=='#':
            continue
        if not outrange(i,j+1,h,w) and mat[i][j+1]=='#':
            continue

        print("No")
        exit(0)
print("Yes")

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [['.']*(W+2)]
for i in range(H):
    S.append(['.'] + list(input()) + ['.'])
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] == '.' and S[i-1][j] == '.' and S[i][j+1] == '.' and S[i][j-1] == '.':
                print('No')
                break
    else:
        continue
    break
else:
    print('Yes')

Submission status: Accepted

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

def check(i, j):
    if g[i][j] == '.':
        return True
    lis = [[-1, 0], [0, -1], [0, 1], [1, 0]]
    f = 0
    for k in lis:
        rh = i + k[0]
        rw = j + k[1]
        if 0 <= rh <= h - 1 and 0 <= rw <= w - 1:
            if g[rh][rw] == '#':
                return True
    if f == 0:
        return False

for i in range(h):
    for j in range(w):
        if not check(i, j):
            print('No')
            exit()

print('Yes')

Submission status: Accepted

Solution code:
#ABC 096 C - Grid Repainting 2
H, W = map(int, input().split())
S = [input() for i in range(H)]

s = [[0] * (W+2) for i in range(H+2)]
for h in range(H):
    for w in range(W):
        s[h+1][w+1] = S[h][w]

c = [[0] * (W+2) for i in range(H+2)]
for h in range(1,H+1):
    for w in range(1,W+1):
        if s[h][w] == '#':
            if s[h][w-1] == '#':
                c[h][w] += 1
                c[h][w-1] +=1
            if s[h][w+1] == '#':
                c[h][w] += 1
                c[h][w+1] +=1
            if s[h+1][w] == '#':
                c[h][w] += 1
                c[h+1][w] +=1
            if s[h-1][w] == '#':
                c[h-1][w] += 1
                c[h-1][w] +=1

for h in range(1,H+1):
    for w in range(1,W+1):
        if s[h][w] == '#':
            if c[h][w] == 0:
                print('No')
                exit()
print('Yes')

Submission status: Accepted

Solution code:
def printField(l):
    h = len(l)
    w = len(l[0])
    for i in range(h):
        for j in range(w):
            print(l[i][j], end='')
        print()


h, w = map(int, input().split())
l = [list(input()) for _ in range(h)]
# print(l)

for i in range(h):
        for j in range(w -1):
            if (l[i][j] != '.' and l[i][j+1] != '.'):
                l[i][j] = '$'
                l[i][j+1] = '$'
# printField(l)
# print('========================')

for j in range(w):
    for i in range(h -1):
        if (l[i][j] != '.' and l[i + 1][j] != '.'):
            l[i][j] = '$'
            l[i + 1][j] = '$'
# printField(l)

flag = False
for sl in l:
    for s in sl:
        if s == '#':
            flag = True
            break
    if flag:
        break

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

Submission status: Accepted

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

diff = [[-1, 0], [1, 0], [0, -1], [0, 1]]
def is_isolated(y, x):
    if Sij[y][x] != "#":
        return False
    for d in diff:
        y2 = y + d[0]
        x2 = x + d[1]
        if y2 >= 0 and y2 < H and x2 >= 0 and x2 < W:
            if Sij[y2][x2] == "#":
                return False
    return True

out = "Yes"
for y in range(H):
    for x in range(W):
        if is_isolated(y, x):
            out = "No"
            break
print(out)

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] == "#":
      d = set()
      if i>0:
        d.add(s[i-1][j])
      if i<h-1:
        d.add(s[i+1][j])
      if j>0:
        d.add(s[i][j-1])
      if j<w-1:
        d.add(s[i][j+1])
      if "#" not in d:
        print("No")
        exit()
print("Yes")

Submission status: Accepted

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

for i in range(h):
	for j in range(w):
		if s[i][j] == '.':
			continue
		#四方に#が入っているかどうか確認していく
		mawari = []
		if i != 0:
			mawari.append(s[i-1][j])
		if i != h-1:
			mawari.append(s[i+1][j])
		if j != 0:
			mawari.append(s[i][j-1])
		if j != w-1:
			mawari.append(s[i][j+1])
		if '#' not in mawari:
			answer = 'No'
			break

print(answer)

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        edge0 = (i == 0)
        edge1 = (i == H - 1)
        edge2 = (j == 0)
        edge3 = (j == W - 1)
        x = j
        if S[i][j] == "#":
            if (S[i - 1][j] == "#" if not edge0 else False) or \
                    (S[i + 1][j] == "#" if not edge1 else "") or \
                    (S[i][j - 1] == "#" if not edge2 else "") or \
                    (S[i][j + 1] == "#" if not edge3 else ""):
                continue
            else:
                print("No")
                break
    else:
        continue
    break
else:
    print("Yes")

Submission status: Accepted

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

def judge():
    for i in range(H):
        for j in range(W):
            if s[i][j] == '.':
                continue
            periphery = []
            if i != 0:
                periphery.append(s[i - 1][j])
            if i != H - 1:
                periphery.append(s[i + 1][j])
            if j != 0:
                periphery.append(s[i][j - 1])
            if j != W - 1:
                periphery.append(s[i][j + 1])
            if '#' not in periphery:
                return False
    return True

print('Yes' if judge() else 'No')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
field = [list(input()) for i in range(H)]
flag = []
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

for i in range(H):
    for j in range(W):
        if field[i][j] == '.':
            continue
        inflag = False
        for k in range(4):
            ni = i + dx[k]
            nj = j + dy[k]
            if ni < 0 or ni >= H or nj < 0 or nj >= W:
                continue
            elif field[ni][nj] == '#':
                inflag = True
        flag.append(inflag)

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
field = [list(input()) for i in range(H)]
flag = []
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

for i in range(H):
    for j in range(W):
        if field[i][j] == '#':
            inflag = [False] * 4
            for k in range(4):
                ni = i + dx[k]
                nj = j + dy[k]
                if ni < 0 or ni >= H or nj < 0 or nj >= W:
                    inflag[k] = False
                elif field[ni][nj] == '#':
                    inflag[k] = True
            flag.append(any(inflag))
        elif field[i][j] == '.':
            flag.append(True)

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

Submission status: Accepted

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

def check(h,w):
  return S[h-1][w] == "#" or \
         S[h+1][w] == "#" or \
         S[h][w-1] == "#" or \
         S[h][w+1] == "#"

for h in range(1,H+1):
  for w in range(1,W+1):
    if S[h][w] == "#" and not check(h,w):
      print("No")
      exit()
print("Yes")

Submission status: Accepted

Solution code:
class UnionFind:
    def __init__(self, N:int):
        """N: 大きさ
        size: 連結成分の大きさ
        """
        self.par = [i for i in range(N)]
        self.size = [1]*N
        self.rank = [1]*N
        
    def root(self, x:int)->int:
        """根を求める"""
        if self.par[x] == x: # if root
            return x
        else:
            self.par[x] = self.root(self.par[x]) # 経路圧縮
            return self.par[x]
        
    def is_same(self, x:int, y:int)->bool:
        """x と y が同じ集合に属するか否か"""
        return self.root(x)==self.root(y)
    
    def unite(self, x:int, y:int):
        """y を x の属する集合に併合"""
        root_x = self.root(x)
        root_y = self.root(y)
        if root_x == root_y: return
        if self.rank[root_x] == self.rank[root_y]:
            self.rank[root_x] += 1
        elif self.rank[root_x]<self.rank[root_y]:
            root_x,root_y = root_y,root_x
        # 短いほう(y)を長いほう(x)にくっつける
        self.par[root_y] = root_x
        self.size[root_x] += self.size[root_y]
        self.size[root_y] = 0 # もういらない
        
    def get_size(self, x:int)->int:
        """xの属するグループのサイズ"""
        return self.size[self.root(x)]

H,W = map(int, input().split())
grid = [input() for _ in range(H)]
uf = UnionFind(H*W)
for h in range(H):
    for w in range(W):
        for i,j in zip([0,0,1,-1], [1,-1,0,0]):
            if not (0 <= h+i < H and 0 <= w+j < W): 
                continue
            if grid[h][w]=="#" and grid[h+i][w+j]=="#":
                uf.unite(h*W + w, (h+i)*W + w+j) 
                
        #print([uf.root(n) for n in range(H*W)])

ans = "Yes"
for h in range(H):
    for w in range(W):
        if grid[h][w]=="#":
            if uf.get_size(h*W + w)<2:
                ans = "No"
print(ans)

Submission status: Accepted

Solution code:
# /user/bin/python
# -*- coding: utf-8 -*-
import sys


def solve(sn,h,w):
  for i in range(1,h+1):
    for j in range(1,w+1):
      if sn[i][j] > 0:
        if sn[i-1][j] + sn[i+1][j] + sn[i][j-1] + sn[i][j+1] == 0:
          return False
  return True



H,W = map(int, input().split())
Sn = []
Sn.append([0]*(W+2))
for _ in range(H):
  s_in = str(input())
  lst = [0] * (W+2)
  for i in range(len(s_in)):
    if s_in[i] == '#':
      lst[i+1] = 1
  Sn.append(lst)
Sn.append([0]*(W+2))

if solve(Sn,H,W):
  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)]
dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]

for i in range(H):
    for j in range(W):
        if(S[i][j]=="."):
            continue
        exist = False
        for k in range(4):
            if(0<=i+dy[k]<H and 0<=j+dx[k]<W and S[i+dy[k]][j+dx[k]]=="#"):
                exist = True
        if(exist==False):
            print("No")
            sys.exit()     
print("Yes")

Submission status: Accepted

Solution code:
h,w=[int(i) for i in input().split()]
s=[[i for i in input()] for _ in range(h)]
ans=True
for i in range(h):
    for j in range(w):
        if s[i][j]=="#":
            black=False
            try:
                if s[i-1][j]=="#":
                    black=True
            except:
                pass
            try:
                if s[i+1][j]=="#":
                    black=True
            except:
                pass
            try:
                if s[i][j-1]=="#":
                    black=True
            except:
                pass
            try:
                if s[i][j+1]=="#":
                    black=True
            except:
                pass
            if not black:
                ans=False
                break
print("Yes" if ans else "No")

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            continue
        flag = 0
        for k in range(4):
            dh = i + dy[k]
            dw = j + dx[k]
            # print(dh, dw)
            if 0 <= dh < h and 0 <= dw < w and s[dh][dw] == '#':
                flag = 1
        if flag == 0:
            print('No')
            sys.exit()
print('Yes')

Submission status: Accepted

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

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

for i in range(h):
    for j in range(w):
        if S[i][j] == '.':
            continue
        else:
            count = 0
            for k in range(4):
                    if not 0<=i+dy[k]<h or not 0<=j+dx[k]<w:
                        continue
                    if S[i+dy[k]][j+dx[k]] == '#':
                        count += 1
            if count == 0:
                print('No')
                exit()

print('Yes')

Submission status: Accepted

Solution code:
import sys

a = list(map(int,input().split()))
H = a[0]
W = a[1]
board = [["d" for j in range(W+2)] for i in range(H+2)]
for i in range(H):
    temp = input()
    for j in range(W):
        board[i+1][j+1] = temp[j]


for i in range(1, H+1):
    for j in range(1, W+1):
        if board[i][j] == "#" and board[i][j-1] != "#" and board[i][j+1] != "#" and board[i-1][j] != "#" and board[i+1][j] != "#":
            print("No")
            sys.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 = [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 k in range(4):
                ny = y + dy[k]
                nx = x + dx[k]

                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(['.' for _ in range(W+2)])
for i in range(H):
    si = list(input())
    si = ['.'] + si + ['.']
    s.append(si)
s.append(['.' for _ in range(W+2)])
flag = True
for h in range(1,H+1):
    for w in range(1,W+1):
        if s[h][w] == '#':
            if s[h-1][w]=='.' and s[h+1][w]=='.' and s[h][w+1]=='.' and s[h][w-1]=='.' :
                flag = False

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

Submission status: Accepted

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

flg = True
for j in range(h):
    for i in range(w):
        if e[j][i] == ".":
            continue
        if i != 0 and e[j][i-1] == "#":
            continue
        if j != 0 and e[j-1][i] == "#":
            continue
        if i != w-1 and e[j][i+1] == "#":
            continue
        if j != h-1 and e[j+1][i] == "#":
            continue
        flg = False
        break

    if not flg:
        break

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

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
s = [input() for i in range(H)]
precount = 0
for i in range(H):
    precount += s[i].count("#")
if precount == 0:
    print("Yes")
else:
    ans = "Yes"
    dx = [1,0,-1,0]
    dy = [0,1,0,-1]
    for i in range(H):
        for j in range(W):
            if s[i][j] == "#":
               count = 0
               for k in range(4):
                   nx = j+dx[k]
                   ny = i+dy[k]
                   if 0 <= nx < W and 0 <= ny < H:
                      if s[ny][nx] == "#":
                         count += 1
               if count == 0:
                  ans = "No"

    print(ans)

Submission status: Accepted

Solution code:
def solve():
    H,W=map(int,input().split())
    s=[]
    s.append(['.'] * (W+2))
    for y in range(H):
        s.append(['.'] + list(input()) + ['.'])
    s.append(['.'] * (W+2))
    for y in range(1,H):
        for x in range(1,W):
            if s[y][x] == '#':
                if s[y-1][x] == '.' and \
                   s[y+1][x] == '.' and \
                   s[y][x-1] == '.' and \
                   s[y][x+1] == '.':
                   return 'No'
    return 'Yes'

print(solve())

Submission status: Accepted

Solution code:
H , W = map(int, input().split())
tile = [[x for x in  input()] for _ in range(H)]
#print(tile)
checktile = [[0] * H] * W
#print(checktile)
flg = True
for i in range(H):
    for j in range(W):
        tempflg = False
        if tile[i][j] == "#":
                #右をチェック、
            if j < W -1:
                if tile[i][j+1] == "#":
                    tempflg = True
                #下をチェック
            if i < H -1:
                if tile[i+1][j] == "#":
                    tempflg = True
            #上をチェック
            if i > 0:
                if tile[i-1][j] == "#":
                    tempflg = True
            #左をチェック
            if j > 0:
                if tile[i][j-1] == "#":
                    tempflg = True
            if tempflg == False:
                #print("i:j:",i,j)
                flg = False
                
print("Yes" if flg else "No")

Submission status: Accepted

Solution code:
def grid_repeating2(H: int, W: int, S: list) -> bool:
    """マスに塗っていい回数の上限がないので、上下左右に黒ます
    がひとつでもあれば OK。つまり、孤立点の有無で判断すればよ
    い。
    """
    for h in range(H):
        for w in range(W):
            if S[h][w] == '.':
                # 白塗り部分は無視
                continue
            # 上下左右のどれかが黒(#)なら OK
            if w > 0 and S[h][w-1] == '#':
                continue
            if w+1 < W and S[h][w+1] == '#':
                continue
            if h > 0 and S[h-1][w] == '#':
                continue
            if h+1 < H and S[h+1][w] == '#':
                continue

            # 上の条件分岐にすべて失敗したなら孤立点
            return False
    return True


if __name__ == "__main__":
    H = 0
    W = 0
    H, W = map(int, input().split())
    S = [input() for _ in range(H)]
    yes = grid_repeating2(H, W, S)
    print('Yes' if yes else 'No')

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
L = []
for i in range(H):
  L.append(list(input()))
dy_dx = [[1,0],[-1,0],[0,1],[0,-1]]
cnt = 0
for i in range(H):
  for j in range(W):
    d = 0
    if L[i][j] == '#':
      for p in range(4):
        if 0 <= i+dy_dx[p][0] <= H-1 and 0 <= j+dy_dx[p][1] <= W-1:
          if L[i+dy_dx[p][0]][j+dy_dx[p][1]] == '.':
            d += 1
      if 1 <= i <= H-2 and 1 <= j <= W-2:
        if d == 4:
          cnt += 1
      elif (i == 0 and j == 0) or (i == 0 and j == W-1) or (i == H-1 and j == 0) or (i == H-1 and j == W-1):
        if d == 2:
          cnt += 1
      else:
        if d == 3:
          cnt += 1
if cnt == 0:
  print('Yes')
else:
  print('No')

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
L = []
for i in range(H):
  L.append(list(input()))
dy_dx = [[1,0],[-1,0],[0,1],[0,-1]]
cnt = 0
for i in range(H):
  for j in range(W):
    d = 0
    if L[i][j] == '#':
      for p in range(4):
        if 0 <= i+dy_dx[p][0] <= H-1 and 0 <= j+dy_dx[p][1] <= W-1:
          if L[i+dy_dx[p][0]][j+dy_dx[p][1]] == '.':
            d += 1
      if 1 <= i <= H-2 and 1 <= j <= W-2:
        if d == 4:
          cnt += 1
      elif i == 0 and j == 0:
        if d == 2:
          cnt += 1
      elif i == 0 and j == W-1:
        if d == 2:
          cnt += 1
      elif i == H-1 and j == 0:
        if d == 2:
          cnt += 1
      elif i == H-1 and j == W-1:
        if d == 2:
          cnt += 1
      else:
        if d == 3:
          cnt += 1
if cnt == 0:
  print('Yes')
else:
  print('No')

Submission status: Accepted

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

ans = "Yes"
for i in range(H-1):
  for j in range(W-1):
    if(C[i][j] == "#"):
      if(i != 0 and C[i-1][j] == "#"):
        continue
      if(i != H and C[i+1][j] == "#"):
        continue
      if(j != 0 and C[i][j-1] == "#"):
        continue
      if(j != W and C[i][j+1] == "#"):
        continue
      ans = "No"
print(ans)

Submission status: Accepted

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

ans = 'Yes'
for i in range(H):
    if ans == 'No':
        break
    for j in range(W):
        if s[i][j] == '.':
            continue
        l = []
        if j>0:
            l.append(s[i][j-1])
        if j<len(s[i])-1:
            l.append(s[i][j+1])
        if i>0:
            l.append(s[i-1][j])
        if i<H-1:
            l.append(s[i+1][j])
        if '#' not in l:
            ans = 'No'
            break

print(ans)

Submission status: Accepted

Solution code:
import sys
def main():
    H,W = map(int, input().split())
    S = [input() for i in range(H)]
    
    cnt = 0
    for i in range(H):
            #W=x=j
        for j in range(W):
            if S[i][j] == ".":
                continue
            else:
                #y増分
                cnt = 0
                for k in range(2):
                    if 0 <= i + (k*2 - 1) < H and S[i+(k*2-1)][j] == '#':
                        cnt = 1
                        break
                    elif 0 <= j + (k*2 -1) < W and S[i][j+(k*2-1)] == '#':
                        cnt = 1
                        break
                if cnt == 0:
                    print("No")
                    exit()
        #S.insert(0,"." * W)
        #S.append("." * W)
    print("Yes")
    
main()

Submission status: Accepted

Solution code:
import numpy as np
def repaint(h,w,s):
    for i in range(1,h+1):
        for p in range(1,w+1):
            if s[i][p] == '#':
                if (s[i-1][p] != '#') and (s[i][p-1] != '#') and (s[i+1][p] != '#') and (s[i][p+1] != '#'):
                    return 'No'
    return 'Yes'
H, W = map(int, input().split())
S = np.array([['0']*(W+2)] + [list('0' + input() + '0') for i in range(H)] +[['0']*(W+2)])
print(repaint(H,W,S))

Submission status: Accepted

Solution code:
H, W = map(int,input().split())
S = [input() for _ in range(H)]
ans = "Yes"
for i in range(H-1):
  for j in range(W-1):
    if(S[i][j] == "#"):
      if(i != 0 and S[i-1][j] == "#"):
        continue
      if(i != H and S[i+1][j] == "#"):
        continue
      if(j != 0 and S[i][j-1] == "#"):
        continue
      if(j != W and S[i][j+1] == "#"):
        continue
      ans = "No"
print(ans)

Submission status: Accepted

Solution code:
h,w = (int(i) for i in 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 or s[i-1][j] == '.':
        if i==h-1 or s[i+1][j] == '.':
          if j==0 or s[i][j-1] == '.':
            if j==w-1 or s[i][j+1] == '.':
              print('No')
              exit()
print('Yes')

Submission status: Accepted

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


if __name__ == '__main__':
    main()