submission_id
string
problem_id
string
status
string
code
string
input
string
output
string
problem_description
string
s068455232
p00066
Accepted
table = [[''] * 3 for i in range(3)] def ans(): r = 0 c = 1 # 横 while(r < 3): char = table[r][0] if char != 's': c = 1 while(c < 3): if table[r][c] != char: break if table[r][c] == char and c + 1 >= 3: return char c += 1 r += 1 # 縦 r = 1 c = 0 while(c < 3): char = table[0][c] if char != 's': r = 1 while(r < 3): if table[r][c] != char: break if table[r][c] == char and r + 1 >= 3: return char r += 1 c += 1 # 斜め char = table[1][1] if char != 's': if (table[0][0] == char and table[2][2] == char) or (table[0][2] == char and table[2][0] == char): return char return 'd' while(1): try: s = input() for i in range(0,3): for j in range(0,3): table[i][j] = s[(i * 3) + j] print(ans()) except: break
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s992287345
p00066
Accepted
def cross(s): if s[0]==s[4]==s[8] and s[0]!="s": print(s[0]) return True elif s[2]==s[4]==s[6] and s[2]!="s": print(s[2]) return True else: return False def side(s): for i in range(0,len(s),3): if s[i]==s[i+1]==s[i+2] and s[i]!="s": print(s[i]) return True return False def ver(s): for i in range(3): if s[i]==s[i+3]==s[i+6] and s[i]!="s": print(s[i]) return True return False while True: try: s=list(map(str,input())) except EOFError: break if cross(s)==side(s)==ver(s)==False: print("d")
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s704832385
p00066
Accepted
while True: try: s = input().rstrip() except: break win = None for i, j, k in ((0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6)): chk = s[i] + s[j] + s[k] if chk == "ooo": win = "o" break elif chk == "xxx": win = "x" break if win: print(win) else: print("d")
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s371308290
p00066
Accepted
def f(s): for t in ['o', 'x']: if s[::4].count(t) == 3 or s[2:7:2].count(t) == 3: return t for i in range(3): if s[i::3].count(t) == 3 or s[3 * i:3 * i + 3].count(t) == 3: return t return 'd' while 1: try: print(f(input())) except: break
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s577532681
p00066
Accepted
if __name__ == '__main__': while True: try: line = list(input()) ans = "d" if line[0] == "o" and line[4] == "o" and line[8] == "o": ans = "o" elif line[2] == "o" and line[4] == "o" and line[6] == "o": ans = "o" elif line[0] == "o" and line[3] == "o" and line[6] == "o": ans = "o" elif line[1] == "o" and line[4] == "o" and line[7] == "o": ans = "o" elif line[2] == "o" and line[5] == "o" and line[8] == "o": ans = "o" elif line[0] == "o" and line[1] == "o" and line[2] == "o": ans = "o" elif line[3] == "o" and line[4] == "o" and line[5] == "o": ans = "o" elif line[6] == "o" and line[7] == "o" and line[8] == "o": ans = "o" if line[0] == "x" and line[4] == "x" and line[8] == "x": ans = "x" elif line[2] == "x" and line[4] == "x" and line[6] == "x": ans = "x" elif line[0] == "x" and line[3] == "x" and line[6] == "x": ans = "x" elif line[1] == "x" and line[4] == "x" and line[7] == "x": ans = "x" elif line[2] == "x" and line[5] == "x" and line[8] == "x": ans = "x" elif line[0] == "x" and line[1] == "x" and line[2] == "x": ans = "x" elif line[3] == "x" and line[4] == "x" and line[5] == "x": ans = "x" elif line[6] == "x" and line[7] == "x" and line[8] == "x": ans = "x" print(ans) except EOFError: break
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s853038754
p00066
Accepted
while 1: try: s=input() if s[0]==s[1]==s[2]!="s": print(s[0]) elif s[3]==s[4]==s[5]!="s": print(s[3]) elif s[6]==s[7]==s[8]!="s": print(s[6]) elif s[0]==s[3]==s[6]!="s": print(s[0]) elif s[1]==s[4]==s[7]!="s": print(s[1]) elif s[2]==s[5]==s[8]!="s": print(s[2]) elif s[0]==s[4]==s[8]!="s": print(s[0]) elif s[2]==s[4]==s[6]!="s": print(s[2]) else: print("d") except:break
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s824367626
p00066
Accepted
while(1): try: faces = [1 if i == "o" else 0 for i in list(input())] hori = [sum(faces[0:3]),sum(faces[3:6]),sum(faces[6:9])] vert = [sum(faces[0:7:3]),sum(faces[1:8:3]),sum(faces[2:9:3])] diag = [sum(faces[0:9:4]),sum(faces[2:7:2])] hori.extend(vert) hori.extend(diag) if 3 in hori: print("o") elif 0 in hori: print("x") else: print("d") except EOFError: break
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s282729449
p00066
Runtime Error
def marubatu(): N=list(input()) for koma in ["o","x"]: for i in range(3): if N[i:9:3].count(koma)==3 or N[3*i:3*i+3].count(koma)==3:return koma if N[0:9:4].count(koma)==3 or N[2:7:2].count(koma)==3:return koma return "d" while 1: print(marubatu())
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s099537838
p00066
Runtime Error
import sys f = sys.stdin vlines = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]] for line in f: result = 'd' for v in vlines: if 's' != a and line[v[0]]== line[v[1]] == line[v[2]]: result = line[v[0]] break print(result)
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s070590442
p00066
Runtime Error
ok = [[0,4,8], [2,4,6], [0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8]] while True: s = input() if len(s) == 0: break maru = [] batu = [] for i in range(9): if s[i] == 'o': maru.append(i) elif s[i] == 'x': batu.append(i) print(maru, batu) flag = False for i in ok: if i == maru: print("o") flag = True break elif i ==batu: print("x") flag = True break if not flag: print("d")
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s375226972
p00066
Runtime Error
ok = [[0,4,8], [2,4,6], [0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8]] while True: s = input() if len(s) == 0: break maru = [] batu = [] for i in range(9): if s[i] == 'o': maru.append(i) elif s[i] == 'x': batu.append(i) flag = False for i in ok: if i == maru: print("o") flag = True break elif i ==batu: print("x") flag = True break if not flag: print("d")
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s057371703
p00066
Runtime Error
ok = [[0,4,8], [2,4,6], [0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8]] while True: s = input() if len(s) == 0: break maru = [] batu = [] for i in range(9): if s[i] == 'o': maru.append(i) elif s[i] == 'x': batu.append(i) flag = False for i in ok: if i == maru: print("o") flag = True break elif i ==batu: print("x") flag = True break if flag: continue print("d")
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s168091562
p00066
Runtime Error
ok = [{0,4,8}, {2,4,6}, {0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,5,8}] while True: s = input() if len(s) == 0: break maru = set() batu = set() for i in range(9): if s[i] == 'o': maru.add(i) elif s[i] == 'x': batu.add(i) flag = False for i in ok: if len(i - maru) == 0: print("o") flag = True break elif len(i - batu) == 0: print("x") flag = True break if flag: continue print("d")
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s039830187
p00066
Runtime Error
ok = [[0,4,8], [2,4,6], [0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8]] while True: s = input() if len(s) == 0: break flag = False for i in ok: if s[i[0]] == s[i[1]] == s[i[2]]: print(s[i[0]]) flag = True if flag: continue print("d")
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s310296365
p00066
Runtime Error
ok = [[0,4,8], [2,4,6], [0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8]] while True: s = input() if len(s) == 0: break flag = False for i in ok: if s[i[0]] == s[i[1]] == s[i[2]]: print(s[i[0]]) flag = True break if flag: continue print("d")
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s968137324
p00066
Runtime Error
ok = [[0,4,8], [2,4,6], [0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8]] while True: s = input() if len(s) == 0: break flag = False for i in ok: if s[i[0]] == s[i[1]] == s[i[2]] and s[i[0]] != 's': print(s[i[0]]) flag = True break if flag: continue print("d")
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s951007491
p00066
Runtime Error
import sys board = [] board_list = [] result = [] while 1: line = input() if not line == "": board.append(x) else: break def check(num): if board_list[num][0] == board_list[num][1] == board_list[num][2]: if board_list[num][0] == 'o': return 'o' elif board_list[num][0] == 'x': return 'x' if board_list[0][num] == board_list[1][num] == board_list[2][num]: if board_list[0][num] == 'o': return 'o' elif board_list[0][num] == 'x': return 'x' if board_list[0][0] == board_list[1][1] == board_list[2][2]: if board_list[0][0] == 'o': return 'o' elif board_list[0][0] == 'x': return 'x' if board_list[0][2] == board_list[1][1] == board_list[2][0]: if board_list[0][0] == 'o': return 'o' elif board_list[0][0] == 'x': return 'x' return'd' for i in board: board_list = [(a + b + c) for (a, b, c) in zip(i[::3], i[1::3], i[2::3])] for j in range(3): if check(j) != "d": result.append(check(j)) break elif j == 2: result.append(check(j)) for i in result: print(i)
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s542407341
p00066
Runtime Error
import sys board = [] board_list = [] result = [] while 1: line = input() if not line == "": board.append(x) else: break except EOFError: pass def check(num): if board_list[num][0] == board_list[num][1] == board_list[num][2]: if board_list[num][0] == 'o': return 'o' elif board_list[num][0] == 'x': return 'x' if board_list[0][num] == board_list[1][num] == board_list[2][num]: if board_list[0][num] == 'o': return 'o' elif board_list[0][num] == 'x': return 'x' if board_list[0][0] == board_list[1][1] == board_list[2][2]: if board_list[0][0] == 'o': return 'o' elif board_list[0][0] == 'x': return 'x' if board_list[0][2] == board_list[1][1] == board_list[2][0]: if board_list[0][0] == 'o': return 'o' elif board_list[0][0] == 'x': return 'x' return'd' for i in board: board_list = [(a + b + c) for (a, b, c) in zip(i[::3], i[1::3], i[2::3])] for j in range(3): if check(j) != "d": result.append(check(j)) break elif j == 2: result.append(check(j)) for i in result: print(i)
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s084165865
p00066
Runtime Error
import sys board = [] board_list = [] result = [] try: while 1: line = input() if not line == "": board.append(x) else: break except EOFError: pass def check(num): if board_list[num][0] == board_list[num][1] == board_list[num][2]: if board_list[num][0] == 'o': return 'o' elif board_list[num][0] == 'x': return 'x' if board_list[0][num] == board_list[1][num] == board_list[2][num]: if board_list[0][num] == 'o': return 'o' elif board_list[0][num] == 'x': return 'x' if board_list[0][0] == board_list[1][1] == board_list[2][2]: if board_list[0][0] == 'o': return 'o' elif board_list[0][0] == 'x': return 'x' if board_list[0][2] == board_list[1][1] == board_list[2][0]: if board_list[0][0] == 'o': return 'o' elif board_list[0][0] == 'x': return 'x' return'd' for i in board: board_list = [(a + b + c) for (a, b, c) in zip(i[::3], i[1::3], i[2::3])] for j in range(3): if check(j) != "d": result.append(check(j)) break elif j == 2: result.append(check(j)) for i in result: print(i)
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s559180362
p00066
Runtime Error
d=0 while True: d+=1 if d==51: break s=str(input()) if s[0]==s[1]==s[2] and s[0] != "s": print(s[0]) elif s[3]==s[4]==s[5] and s[3] != "s": print(s[3]) elif s[6]==s[7]==s[8] and s[6] != "s": print(s[6]) elif s[0]==s[4]==s[8] and s[0] != "s": print(s[0]) elif s[2]==s[4]==s[6] and s[2] != "s": print(s[2]) elif s[0]==s[3]==s[6] and s[0] != "s": print(s[0]) elif s[1]==s[4]==s[7] and s[1] != "s": print(s[1]) elif s[2]==s[5]==s[8] and s[2] != "s": print(s[2]) else: print("d")
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s725179476
p00066
Runtime Error
ls = ["o","x"] while True: try: a = raw_input() for c in ls if a[::4] == c*3 or a[2:8:2] == c*3: print c else: for i in range(3): if a[3*i:3*(i+1)] == c*3 or a[i::3] == c*3: print c break elif i == 2: print "d" except: break
ooosxssxs xoosxsosx ooxxxooxo
o x d
<H1>三目並べ</H1> <p> 三目並べは,3 &times; 3のマス目の中に交互に ○ と × を入れていき、縦・横・斜めの何れかに一列 ○ か × が並んだときに、勝ちとなるゲームです(図1〜図3 を参照) </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic1"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic2"></td> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic3"></td> </tr> <tr> <td>図1:○の勝ち</td> <td>図2:× の勝ち</td> <td>図3:引き分け</td> </tr> </table> </center> <br/> <p> 三目並べは、○と×が交互にマス目を埋めていき、どちらかが一列揃ったときにゲーム終了となります。そのため、図 4 のように、○と×が両方とも一列そろっている場合はありえない局面です。ありえない局面が入力されることはありません。 </p> <center> <table> <tr> <td width="140"><img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic4"></td> </tr> <tr> <td>図4:ありえない局面</td> </tr> </table> </center> <br/> <p> 三目並べの盤面を読み込んで、勝敗の結果を出力するプログラムを作成して下さい。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットとして、盤面を表す1つの文字列が1行に与えられます。 盤面の文字列は、○、×、空白をそれぞれ半角英小文字の o、x、s であらわし、下図のマス目の順に並んでいます。 </p> <center> <img src="https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE1_tic5"> </center> <br/> <p> データセットの数は 50 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、○が勝ちなら半角英小文字の o を、×が勝ちなら半角英小文字の x を、引き分けならば半角英小文字の d を1行に出力してください。 </p> <H2>Sample Input</H2> <pre> ooosxssxs xoosxsosx ooxxxooxo </pre> <H2>Output for the Sample Input</H2> <pre> o x d </pre>
s826748773
p00067
Wrong Answer
import itertools def getPoint(m,x,y): if not (0 <= x < 12) or not (0 <= y < 12): return 0 return m[y][x] def setPoint(m,x,y,v): m[y][x] = v while 1: try: m = [list(map(int,input().strip())) for _ in range(12)] count = 0 empty = False while not empty: empty = True for x,y in itertools.product(range(12),repeat=2): if(getPoint(m,x,y)): print(x,y) count += 1 empty = False q = [] q.append((x,y)) while q != []: p = q.pop() if(getPoint(m,p[0],p[1])): setPoint(m,p[0],p[1],0) for o in [(1,0),(-1,0),(0,1),(0,-1)]: q.append((p[0] + o[0], p[1] + o[1])) print(count) input() except: exit()
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s507255947
p00067
Wrong Answer
def fill(x, y, board): board[y][x] = 2 points = [[y, x + 1], [y, x - 1], [y + 1, x], [y - 1, x]] # y if y == 0: points.remove([y-1, x]) elif y == 11: points.remove([y+1, x]) # x if x == 0: points.remove([y, x - 1]) elif x == 11: points.remove([y, x + 1]) for p in points: if board[p[0]][p[1]] == 1: board = fill(p[1], p[0], board) return board islands = [list(map(int, list(input()))) for i in range(12)] ans = 0 for y in range(12): for x in range(12): if islands[y][x] == 1: islands = fill(x, y, islands) ans += 1 print(ans)
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s691783180
p00067
Wrong Answer
def fill(x, y, board): board[y][x] = 2 points = [[y, x + 1], [y, x - 1], [y + 1, x], [y - 1, x]] # y if y == 0: points.remove([y-1, x]) elif y == 11: points.remove([y+1, x]) # x if x == 0: points.remove([y, x - 1]) elif x == 11: points.remove([y, x + 1]) for p in points: if board[p[0]][p[1]] == 1: board = fill(p[1], p[0], board) return board while True: try: islands = [list(map(int, list(input()))) for i in range(12)] input() ans = 0 for y in range(12): for x in range(12): if islands[y][x] == 1: islands = fill(x, y, islands) ans += 1 print(ans) except: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s196584610
p00067
Wrong Answer
import sys def quartet(h, w): global flag if 0 <= h <= 11 and 0 <= w <= 11: if mapp[h][w] == '1': mapp[h][w] = '$' flag = True bigbang(h, w) elif mapp[h][w] == '2' or mapp[h][w] == '$': return else: return def bigbang(h, w): quartet(h, w+1) quartet(h-1,w) quartet(h, w-1) quartet(h+1, w) mapp = [] for line in sys.stdin: lis = [] for char in line.rstrip(): lis.append(char) else: mapp.append(lis) flag = False count = 0 for h in range(12): for w in range(12): if quartet(h, w): count += 1 else: print count
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s489818149
p00067
Wrong Answer
import sys def quartet(h, w): global flag if 0 <= h <= 11 and 0 <= w <= 11: if mapp[h][w] == '1': mapp[h][w] = '$' flag = True bigbang(h, w) elif mapp[h][w] == '0' or mapp[h][w] == '$': return else: return def bigbang(h, w): quartet(h, w+1) quartet(h-1,w) quartet(h, w-1) quartet(h+1, w) mapp = [] for line in sys.stdin: lis = [] for char in line.rstrip(): lis.append(char) else: mapp.append(lis) flag = False count = 0 for h in range(12): for w in range(12): if quartet(h, w): count += 1 else: print count
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s948598783
p00067
Wrong Answer
import sys def quartet(h, w): global flag if 0 <= h <= 11 and 0 <= w <= 11: if mapp[h][w] == '1': mapp[h][w] = '$' flag = True bigbang(h, w) elif mapp[h][w] == '0' or mapp[h][w] == '$': return else: return def bigbang(h, w): quartet(h, w+1) quartet(h-1,w) quartet(h, w-1) quartet(h+1, w) mapp = [] for line in sys.stdin: lis = [] for char in line.rstrip(): lis.append(char) else: mapp.append(lis) flag = False count = 0 for h in range(12): for w in range(12): quartet(h, w) if flag == True: count += 1 flag = False else: print count
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s619986437
p00067
Wrong Answer
def f1(): t = 0 for y, line in enumerate(M): for x, cell in enumerate(line): if M[y][x] == '1': f2(x, y) t += 1 return t def f2(x, y): if x < 0 or len(M[0]) == x or y < 0 or len(M) == y: return if M[y][x] == '1': M[y][x] = '0' f2(x, y-1) f2(x, y+1) f2(x+1, y) f2(x-1, y) M = [] while True: try: line = input() if line == '': print(f1()) M = [] else: M.append(list(input())) except EOFError: print(f1()) break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s967285222
p00067
Wrong Answer
class Depth_First_Search_stack: def __init__(self, map): self.map = map self.num_island = 0 self.stack = [] self.start_point = None def get_start_point(self): get_start = False for i in range(12): for j in range(12): if self.map[i][j] == '1': self.start_point = [i, j] get_start = True return get_start def check(self, new_r, new_c): return 0<=new_r<12 and 0<=new_c<12 and self.map[new_r][new_c] == '1' def search(self, r, c): while self.stack: r, c = self.stack.pop() self.map[r][c] = '0' for (i, j) in [[1, 0], [0, 1], [-1, 0], [0, -1]]: if self.check(r+i, c+j): self.stack.append([r+i, c+j]) def run(self): while self.get_start_point(): r, c = self.start_point self.stack.append([r, c]) self.search(r, c) self.num_island += 1 if __name__ == '__main__': map = [] for i in range(12): row = [x for x in input()] map.append(row) DFS = Depth_First_Search_stack(map) DFS.run() print(DFS.num_island)
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s696988121
p00067
Wrong Answer
def remove_linked(x, y, DATA): DATA[x][y]=0# clear move = [[1, 0], [0, 1], [-1, 0], [0, -1]]#left, right, down, up for i, j in move: nx,ny=x+i,y+j#next x and y if -1<nx<12 and -1<ny<12 and DATA[nx][ny]: DATA=remove_linked(nx, ny, DATA) return DATA DATA = [[int(x) for x in list(input())] for _ in range(12)] count = 0 for x in range(12): for y in range(12): if DATA[x][y]: count += 1 DATA = remove_linked(x, y, DATA) print (count)
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s391225341
p00067
Wrong Answer
def counter():#島の数をカウントする count=0 #12*12個の値をひとつずつ調べる for x in range(12): for y in range(12): if is_data[x][y] == 1:#島が発見されたら dfs(x,y) count+=1 return count def dfs(x,y): if x<0 or x == len(is_data[x]) or y<0 or y == len(is_data): return if is_data[x][y] == 1: is_data[x][y] = 0 dfs(x+1,y) dfs(x,y+1) dfs(x-1,y) dfs(x,y-1) try: is_data = [] while True: tmp = list(input()) if len(tmp)!=12 :break is_data.append(tmp) except EOFError: print(counter())
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s886576742
p00067
Wrong Answer
def counter():#島の数をカウントする count=0 #12*12個の値をひとつずつ調べる for x in range(12): for y in range(12): if is_data[x][y] == 1:#島が発見されたら dfs(x,y) count+=1 return count def dfs(x,y): if x<0 or x == len(is_data[x]) or y<0 or y == len(is_data): return if is_data[x][y] == 1: is_data[x][y] = 0 dfs(x+1,y) dfs(x,y+1) dfs(x-1,y) dfs(x,y-1) def get_map(): is_data = [] while True: try: tmp = list(int(input())) if len(tmp) != 12: break is_data.append(tmp) except: break return is_data try: while True: is_data = get_map() if len(is_data) != 12: break except EOFError: print(counter())
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s039790576
p00067
Wrong Answer
def counter():#島の数をカウントする count=0 #12*12個の値をひとつずつ調べる for x in range(12): for y in range(12): if is_data[x][y] == 1:#島が発見されたら dfs(x,y) count+=1 return count def dfs(x,y): if x<0 or x == len(is_data[x]) or y<0 or y == len(is_data): return if is_data[x][y] == 1: is_data[x][y] = 0 dfs(x+1,y) dfs(x,y+1) dfs(x-1,y) dfs(x,y-1) def get_map(): land_data = [] while True: try: tmp = list(int(input())) if len(tmp) != 12: break land_data.append(tmp) except: break return land_data try: while True: is_data = get_map() if len(is_data) != 12: break except EOFError: print(counter())
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s922321338
p00067
Wrong Answer
def counter():#島の数をカウントする count=0 #12*12個の値をひとつずつ調べる for x in range(12): for y in range(12): if is_data[x][y] == 1:#島が発見されたら dfs(x,y) count+=1 return count def dfs(x,y): if x<0 or x == len(is_data[x]) or y<0 or y == len(is_data): return if is_data[x][y] == 1: is_data[x][y] = 0 dfs(x+1,y) dfs(x,y+1) dfs(x-1,y) dfs(x,y-1) try: is_data=[] while True: s = input()#入力 if not s :#空入力のときループぬける break is_data.append(list(map(int,s))) except EOFError: print(counter())
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s969457703
p00067
Wrong Answer
def counter():#島の数をカウントする count=0 #12*12個の値をひとつずつ調べる for x in range(12): for y in range(12): if is_data[x][y] == 1:#島が発見されたら dfs(x,y) count+=1 return count def dfs(x,y): if x<0 or x == len(is_data[x]) or y<0 or y == len(is_data): return if is_data[x][y] == 1: is_data[x][y] = 0 dfs(x+1,y) dfs(x,y+1) dfs(x-1,y) dfs(x,y-1) try: is_data=[] while True: s = list(input())#入力 if not s :#空入力のときループぬける break is_data.append(s) except EOFError: print(counter())
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s688026734
p00067
Wrong Answer
def counter():#島の数をカウントする count=0 #12*12個の値をひとつずつ調べる for x in range(len(is_data)): for y in range(len(is_data)): if is_data[x][y] == '1':#島が発見されたら dfs(x,y) count+=1 return count def dfs(x,y): if x<0 or x == len(is_data) or y<0 or y == len(is_data): return if is_data[x][y] == '1': is_data[x][y] = '0' dfs(x+1,y) dfs(x,y+1) dfs(x-1,y) dfs(x,y-1) is_data=[] while True: try: s = list(input())#入力 if not s:#空入力のとき #print(counter()) is_data=[] s = [] else: is_data.append(s) except EOFError: print(counter()) break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s984572968
p00067
Wrong Answer
# AOJ 0067 The Number of Island # Python3 2018.6.16 bal4u # UNION-FIND library MAX = 12*12 id, size = [0]*MAX, [0]*MAX def init(n): for i in range(n): id[i], size[i] = i, 1 def root(i): while i != id[i]: id[i] = id[id[i]] i = id[i] return i def connected(p, q): return root(p) == root(q) def unite(p, q): i, j = root(p), root(q) if i == j: return if size[i] < size[j]: id[i] = j size[j] += size[i] else: id[j] = i size[i] += size[j] # UNION-FIND library arr = [[0 for r in range(12)] for c in range(12)] dr = [-1,0,1, 0 ] dc = [ 0,1,0,-1 ] while True: init(12*12) for r in range(12): arr[r] = list(map(int, input())) for r in range(12): print('r=', arr[r]) for r in range(12): for c in range(12): if arr[r][c] == 0: continue for i in range(4): nr, nc = r + dr[i], c + dc[i] if nr >= 0 and nr < 12 and nc >= 0 and nc < 12: if arr[nr][nc] == 1: unite(r*12+c, nr*12+nc) ans = 0 for r in range(12): for c in range(12): if arr[r][c] == 1 and root(r*12+c) == r*12+c: ans += 1 print(ans) try: input() except: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s481076349
p00067
Wrong Answer
import sys class UnionFind(object): def __init__(self, size): self.data = [-1 for i in range(size)]; def unite(self, x, y): x = self.root(x); y = self.root(y); if (x != y): if (self.data[y] < self.data[x]): x, y = y, x; self.data[x] += self.data[y]; self.data[y] = x; return x != y; def same(self, x, y): return self.root(x) == self.root(y) def root(self, x): if self.data[x] < 0 : return x; self.data[x] = self.root(self.data[x]); return self.data[x]; def size(self, x): return -self.data[self.root(x)]; def pos(i, j): return i * 14 + j while True: uf = UnionFind(14 * 14); board = []; while True: try: s = raw_input(); except: sys.exit(0); s = "0" + s.strip() + "0"; if (s == "00"): break; board.append(list(s)); sent = [0 for i in range(len(board[0]))] board = [sent] + board + [sent]; #print board island = 0 for i in range(1, len(board) - 1): for j in range(1, len(board[0]) - 1): if board[i][j] == '1': p = pos(i, j); f = False; if (board[1 + i][j] == '1'): uf.unite(p, pos(i + 1, j)); f = True; if (board[i - 1][j] == '1'): uf.unite(p, pos(i - 1, j)); f = True; if (board[i][j + 1] == '1'): uf.unite(p, pos(i, j + 1)); f = True; if (board[i][j - 1] == '1'): uf.unite(p, pos(i, j - 1)); f = True; if not f: island += 1 print island + len(filter(lambda x: x < -1, uf.data));
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s415986275
p00067
Wrong Answer
l = 12 def island(f,x,y,m): rf = f[:][:] if rf[y][x] == 1: rf[y][x] = m for i in [-1,1]: if 0 <= x+i <= l-1: rf = island(rf,x+i,y,m) if 0 <= y+i <= l-1: rf = island(rf,x,y+i,m) return rf while True: m, f =2, [] for i in range(l): f.append(map(int, list(raw_input()))) for y in range(l): for x in range(l): f = island(f,x,y,m) m += 1 ans = [] for i in range(l): for j in range(l): ans.append(f[i][j]) print len(list(set(ans)))-1 try : raw_input() except: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s644421093
p00067
Wrong Answer
W,H = 12,12 dxy = [[1,0],[0,1],[-1,0],[0,-1]] def solve(field,w,h): if 0 <= w < W and 0 <= h < H and field[h][w] == 1: field[h][w] = 0 for dx,dy in dxy: solve(field,w+dx,h+dy) while 1: try: field = [map(int,raw_input().split()) for i in range(H)] ans = 0 for h in range(H): for w in range(W): if field[h][w] == 1: solve(field,w,h) ans += 1 print ans except: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s552207827
p00067
Memory Limit Exceeded
class Depth_First_Search_stack: def __init__(self, map): self.map = map self.num_island = 0 self.stack = [] self.start_point = None def get_start_point(self): get_start = False for i in range(12): for j in range(12): if self.map[i][j] == '1': self.start_point = [i, j] get_start = True return get_start def check(self, new_r, new_c): return 0<=new_r<12 and 0<=new_c<12 def search(self, r, c): self.stack.append(self.start_point) while self.stack: r, c = self.stack.pop() self.map[r][c] = '0' for (i, j) in [[1, 0], [0, 1], [-1, 0], [0, -1]]: if self.check(r+i, c+j): self.stack.append([r+i, c+j]) def run(self): while self.get_start_point(): r, c = self.start_point self.search(r, c) self.num_island += 1 if __name__ == '__main__': for t in range(20): stop_flag = False map = [] for i in range(12): row = [x for x in input()] if i==0: if len(row) != 12: stop_flag = True break map.append(row) if stop_flag: break DFS = Depth_First_Search_stack(map) DFS.run() print(DFS.num_island) empty = input()
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s081098074
p00067
Accepted
from collections import deque dq = deque() append, popleft = dq.append, dq.popleft try: while True: a = [[0]*14] + [[0]+list(map(int,input()))+[0] for _ in [0]*12] + [[0]*14] result = 0 for y in range(1, 13): for x in filter(lambda x: a[y][x], range(1, 13)): result += 1 a[y][x] = 0 dq.clear() append((x, y)) while dq: cx, cy = popleft() for dx, dy in ((0, -1), (1, 0), (0, 1), (-1, 0)): if a[cy+dy][cx+dx]: a[cy+dy][cx+dx] = 0 append((cx+dx, cy+dy)) print(result) input() except EOFError: pass
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s793887363
p00067
Accepted
c=0 def f(a,x,y): if 0<=x<12 and 0<=y<12 and a[y][x]=='1': a[y][x]='0' for dx,dy in [[-1,0],[1,0],[0,-1],[0,1]]:f(a,x+dx,y+dy) while 1: try: if c:input() except:break a = [list(input()) for _ in [0]*12] c=1;b=0 for i in range(12): for j in range(12): if a[j][i]=='1': b+=1;f(a,i,j) print(b)
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s397588046
p00067
Accepted
import itertools def getPoint(m,x,y): if not (0 <= x < 12) or not (0 <= y < 12): return 0 return m[y][x] def setPoint(m,x,y,v): m[y][x] = v while 1: try: m = [list(map(int,input().strip())) for _ in range(12)] count = 0 empty = False while not empty: empty = True for x,y in itertools.product(range(12),repeat=2): if(getPoint(m,x,y)): count += 1 empty = False q = [] q.append((x,y)) while q != []: p = q.pop() if(getPoint(m,p[0],p[1])): setPoint(m,p[0],p[1],0) for o in [(1,0),(-1,0),(0,1),(0,-1)]: q.append((p[0] + o[0], p[1] + o[1])) print(count) input() except: exit()
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s853443696
p00067
Accepted
def fill(x, y, board): board[y][x] = 2 points = [[y, x + 1], [y, x - 1], [y + 1, x], [y - 1, x]] if y == 0: points.remove([y-1, x]) elif y == 11: points.remove([y+1, x]) if x == 0: points.remove([y, x - 1]) elif x == 11: points.remove([y, x + 1]) for p in points: if board[p[0]][p[1]] == 1: board = fill(p[1], p[0], board) return board while True: try: islands = [list(map(int, list(input()))) for i in range(12)] ans = 0 for y in range(12): for x in range(12): if islands[y][x] == 1: islands = fill(x, y, islands) ans += 1 print(ans) input() except: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s116709657
p00067
Accepted
import itertools import sys def delete(ya,xa,lis): lis[ya][xa] = 0 move = [[-1,0],[0,-1],[1,0],[0,1]] for x,y in move: if 0<=xa+x<=11 and 0<=ya+y<=11 and lis[ya+y][xa+x]=='1': delete(ya+y,xa+x,lis) def sol(a,b,lis): for i,j in itertools.product(xrange(12),repeat=2): if lis[i][j]=='1': m[i][j] = 1 delete(i,j,lis) while True: try: l = [[ 0 for i in xrange(12) ] for j in xrange(12) ] m = [[ 0 for i in xrange(12) ] for j in xrange(12) ] d = [] for i in xrange(12): d.append(list(raw_input())) sol(0,0,d) print sum([1 for e in m for f in e if f]) raw_input() except: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s279389881
p00067
Accepted
#! /usr/bin/python # -*- coding: utf-8 -*- MAX_Y = 12 MAX_X = 12 XY = ((-1, 0), (1, 0), (0, -1), (0, 1)) def main(): # input while(True): try: data = [] for j in range(MAX_Y): input_list = map(int, raw_input()) data.append(input_list) print(countIsland(data)) raw_input() except EOFError: break def countIsland(data): cnt = [0] def countIsland_(data): land = findIsland(data) if len(land) == 0: return cnt[0] else: data = sinkIsland(data, land) cnt[0] = cnt[0] + 1 return countIsland_(data) return countIsland_(data) # sink adjasent Island. def sinkIsland(data, land): if isIn(*land): if not data[land[1]][land[0]] == 0: data[land[1]][land[0]] = 0 lands = [(k[0] + land[0], k[1] + land[1]) for k in XY] for x in lands: data = sinkIsland(data, x) return data def findIsland(data): for y in range(MAX_Y): for x in range(MAX_X): if data[y][x] == 1: return (x, y) else: return () # decision inner board. def isIn(x, y): return 0<=x<MAX_X and 0<=y<MAX_Y if __name__=='__main__': main()
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s417762700
p00067
Accepted
import sys f = sys.stdin def paintout0(islands, x, y): if 0 <= x <12 and 0 <= y <12 and islands[y][x]: islands[y][x] = 0 paintout0(islands,x + 1,y) paintout0(islands,x,y + 1) paintout0(islands,x -1,y) paintout0(islands,x,y -1) while True: islands = [[int(c) for c in f.readline().strip()] for _ in range(12)] cnt =0 for y in range(len(islands)): for x in range(len(islands[0])): if islands[y][x]: cnt += 1 paintout0(islands,x,y) print(cnt) if not f.readline(): break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s211155757
p00067
Accepted
import sys def quartet(h, w): global flag if 0 <= h <= 11 and 0 <= w <= 11: if mapp[h][w] == '1': mapp[h][w] = '$' flag = True bigbang(h, w) elif mapp[h][w] == '0' or mapp[h][w] == '$': return else: return def bigbang(h, w): quartet(h, w+1) quartet(h-1,w) quartet(h, w-1) quartet(h+1, w) mapp = [] for line in sys.stdin: if not line == '\n': lis = [] for char in line.rstrip(): lis.append(char) else: mapp.append(lis) if len(mapp) == 12: flag = False count = 0 for h in range(12): for w in range(12): quartet(h, w) if flag == True: count += 1 flag = False else: print count mapp = []
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s709249287
p00067
Accepted
def fact(y, x1, x2): island[y] = island[y][:x1] + "0" + island[y][x2:] if island[y-1][x1:x2] == "1": fact(y-1, x1, x2) if island[y+1][x1:x2] == "1": fact(y+1, x1, x2) if island[y][x1-1:x2-1] == "1": fact(y, x1 - 1, x2 - 1) if island[y][x1+1:x2+1] == "1": fact(y, x1 + 1, x2 + 1) while True: island = [] for _ in xrange(12): island.append(raw_input()) island.insert(0, "00000000000000") island.append("00000000000000") for i in xrange(12): island[i+1] = "0" + island[i+1] + "0" count = 0 for i in xrange(12): for j in xrange(12): if island[i+1][j+1:j+2] == "1": fact(i+1, j+1, j+2) count += 1 print count try: raw_input() except EOFError: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s171004292
p00067
Accepted
xy=[(1,0),(-1,0),(0,1),(0,-1)] bs=[(-1,-1),(-1,1),(1,1),(1,-1)] def gcd(a,b): return a if b==0 else gcd(b,a%b) def lcm(a,b): return a*b/gcd(a,b) def choco(xa,ya,xb,yb,xc,yc,xd,yd): return 1 if abs((yb-ya)*(yd-yc)+(xb-xa)*(xd-xc))<1.e-10 else 0 def sol(l,a,b): q=[[a,b]] while len(q): chk=q.pop() for y,x in xy: try: t1,t2=chk[0]+y,chk[1]+x if l[t1][t2]==1 and 0<=t1<12 and 0<=t2<12: q.append([t1,t2]) l[t1][t2]=0 except: pass return l while 1: l=[] try: for i in range(12): l.append([int(x) for x in raw_input()]) ans=0 for i in range(12): for j in range(12): if l[i][j]==1: l[i][j]=0 ans+=1 l=sol(l,i,j) print ans raw_input() except: break exit()
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s568007095
p00067
Accepted
N = 12 di = [1, 0, -1, 0] dj = [0, 1, 0, -1] def dfs(i, j): field[i][j] = '0' for k in xrange(4): ni = i + di[k] nj = j + dj[k] if 0 <= ni < N and 0 <= nj < N and field[ni][nj] == '1': dfs(ni, nj) return; while True: try: field = [] for i in xrange(N): field.append(list(raw_input())) cnt = 0 for i in xrange(N): for j in xrange(N): if field[i][j] == '1': dfs(i, j) cnt += 1 print cnt raw_input() except: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s041841102
p00067
Accepted
def search_neighbor(sea, x, y): global dir_x, dir_y for i in range(4): nx, ny = x + dir_x[i], y + dir_y[i] if not 0 <= nx < 12 or not 0 <= ny < 12: continue if sea[ny][nx]: sea[ny][nx] = 0 search_neighbor(sea, nx, ny) dir_x = (1, -1, 0, 0) dir_y = (0, 0, 1, -1) while True: sea = [[int(i) for i in input()] for _ in range(12)] count = 0 for i in range(144): x, y = i % 12, i // 12 if sea[y][x]: sea[y][x] = 0 search_neighbor(sea, x, y) count += 1 print(count) try: input() except: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s402731869
p00067
Accepted
def f1(): t = 0 for y, line in enumerate(M): for x, cell in enumerate(line): if M[y][x] == '1': f2(x, y) t += 1 return t def f2(x, y): if x < 0 or len(M[0]) == x or y < 0 or len(M) == y: return if M[y][x] == '1': M[y][x] = '0' for i in range(4): if i == 0: # U f2(x, y-1) elif i == 1: # D f2(x, y+1) elif i == 2: # R f2(x+1, y) elif i == 3: # L f2(x-1, y) def get_input(): while True: try: yield input() except EOFError: break M = [] for line in list(get_input()): if line == '': print(f1()) M = [] else: M.append(list(line)) print(f1())
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s147898781
p00067
Accepted
def f1(): t = 0 for y, line in enumerate(M): for x, cell in enumerate(line): if M[y][x] == '1': f2(x, y) t += 1 return t def f2(x, y): if x < 0 or len(M[0]) == x or y < 0 or len(M) == y: return if M[y][x] == '1': M[y][x] = '0' f2(x, y-1) f2(x, y+1) f2(x+1, y) f2(x-1, y) def get_input(): while True: try: yield input() except EOFError: break M = [] for line in list(get_input()): if line == '': print(f1()) M = [] else: M.append(list(line)) print(f1())
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s663956071
p00067
Accepted
def f1(): t = 0 for y, line in enumerate(M): for x, cell in enumerate(line): if M[y][x] == '1': f2(x, y) t += 1 return t def f2(x, y): if x < 0 or len(M[0]) == x or y < 0 or len(M) == y: return if M[y][x] == '1': M[y][x] = '0' f2(x, y-1) f2(x, y+1) f2(x+1, y) f2(x-1, y) M = [] while True: try: line = input() if line == '': print(f1()) M = [] else: M.append(list(line)) except EOFError: print(f1()) break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s482470032
p00067
Accepted
def f1(): t = 0 for y in range(len(M)): for x in range(len(M[0])): if M[y][x] == '1': f2(x, y) t += 1 return t def f2(x, y): if x < 0 or len(M[0]) == x or y < 0 or len(M) == y: return if M[y][x] == '1': M[y][x] = '0' f2(x, y-1) f2(x, y+1) f2(x+1, y) f2(x-1, y) M = [] while True: try: line = input() if line == '': print(f1()) M = [] else: M.append(list(line)) except EOFError: print(f1()) break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s053179492
p00067
Accepted
def check(y,x): if 0<=y<=11 and 0<=x<=11: return True return False def dfs(y,x): if g[y][x]=="0":return 0 g[y][x]="0" for i in xrange(4): nexty=y+dy[i] nextx=x+dx[i] if not check(nexty,nextx):continue if visited[nexty][nextx]==0 and g[nexty][nextx]=="1": visited[nexty][nextx]=1 dfs(nexty,nextx) return 1 dx=(1,0,-1,0) dy=(0,1,0,-1) while 1: try: g=[[] for _ in xrange(12)] for i in xrange(12): for j in raw_input(): g[i].append(j) cnt=0 visited=[[0]*12 for _ in xrange(12)] for i in xrange(12): for j in xrange(12): cnt+=dfs(i,j) print(cnt) raw_input() except: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s316488390
p00067
Accepted
H, W = 12, 12 dxy = [[1, 0], [0, 1], [-1, 0], [0, -1]] field = [] def solver(x, y): global field if x < 0 or y < 0 or x >= H or y >= W: return for dx, dy in dxy: if x+dx < 0 or y+dy < 0 or x+dx >= H or y+dy >= W: continue if field[x+dx][y+dy] == 1: field[x+dx][y+dy] = 0 solver(x+dx, y+dy) while True: field = [] for i in xrange(H): field.append(map(int, raw_input())) ans = 0 for x in xrange(H): for y in xrange(W): if field[x][y] == 1: solver(x, y) ans += 1 print ans try: raw_input() except EOFError: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s717153741
p00067
Accepted
import sys def solve(f, i, j): if 0<=i<12 and 0<=j<12 and f[j][i] == 1: f[j][i] = 0 for dx, dy in [[0,1],[0,-1],[1,0],[-1,0]]: solve(f, i+dx, j+dy) while True: field = [[int(c) for c in sys.stdin.readline().strip()] for _ in range(12)] ans = 0 for i in range(12): for j in range(12): if field[j][i] == 1: solve(field,i,j) ans += 1 print(ans) if not sys.stdin.readline(): break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s188360211
p00067
Accepted
c=0 def f(a,x,y): if 0<=x<12 and 0<=y<12 and a[y][x]=='1': a[y][x]='0' for dx,dy in [[-1,0],[1,0],[0,-1],[0,1]]:f(a,x+dx,y+dy) while 1: try: if c:input() except:break a = [list(input()) for _ in [0]*12] c=1;b=0 for i in range(12): for j in range(12): if a[j][i]=='1': b+=1;f(a,i,j) print(b)
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s425044078
p00067
Accepted
def f(a,x,y): if 0<=x<12 and 0<=y<12 and a[y][x]=='1': a[y][x]='0' for dx,dy in [[-1,0],[1,0],[0,-1],[0,1]]:f(a,x+dx,y+dy) while 1: try: a=[list(input()) for _ in [0]*12] b=0 for i in range(12): for j in range(12): if a[j][i]=='1': b+=1;f(a,i,j) print(b);input() except:break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s347572725
p00067
Accepted
#!usr/bin/env python3 import sys def eliminate_ones(grid, row, col): if 0 <= row < 12 and 0 <= col < 12 and grid[row][col] == 1: grid[row][col] = 0 eliminate_ones(grid, row+1, col) eliminate_ones(grid, row-1, col) eliminate_ones(grid, row, col+1) eliminate_ones(grid, row, col-1) def main(): while True: grid = [[int(num) for num in sys.stdin.readline().strip()] for _ in range(12)] num_of_islands = int() for row in range(len(grid)): for col in range(len(grid[row])): if grid[row][col] == 1: num_of_islands += 1 eliminate_ones(grid, row, col) print(num_of_islands) if not sys.stdin.readline(): break if __name__ == '__main__': main()
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s264164022
p00067
Accepted
def dfs(i, j, m): ''' m[i][j]??¨????????£??????????????°???????????????????????? ''' m[i][j] = 0 for di, dj in [(-1, 0), (1, 0), (0, -1), (0, 1)]: ni = i + di nj = j + dj if ni in range(12) and nj in range(12) and m[ni][nj] == 1: dfs(ni, nj, m) while True: try: m = [list(map(int, list(input()))) for _ in range(12)] count = 0 for i in range(12): for j in range(12): if m[i][j] == 1: count += 1 dfs(i, j, m) print(count) input() except: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s303849829
p00067
Accepted
N = 12 dx = [0,1,0,-1] dy = [-1,0,1,0] def dfs(y,x,vis,S): vis [y] [x] = True for i in range(len(dx)): ny = y + dy [i] nx = x + dx [i] if (ny >= 0 and ny < N and nx >= 0 and nx < N) == False: continue if S [ny] [nx] == '1' and vis [ny] [nx] == False: dfs(ny,nx,vis,S) while True: S = [] for i in range(N): S.append(input()) ans = 0 vis = [[False] * N for i in range(N)] for i in range(N): for j in range(N): if S [i] [j] == '1' and vis [i] [j] == False: dfs(i,j,vis,S) ans += 1 print(ans) try: input() except EOFError: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s860695270
p00067
Accepted
def remove_linked(x, y, DATA): DATA[x][y]=0# clear move = [[1, 0], [0, 1], [-1, 0], [0, -1]]#left, right, down, up for i, j in move: nx,ny=x+i,y+j#next x and y if -1<nx<12 and -1<ny<12 and DATA[nx][ny]: DATA=remove_linked(nx, ny, DATA) return DATA while 1: DATA = [[int(x) for x in list(input())] for _ in range(12)] count = 0 for x in range(12): for y in range(12): if DATA[x][y]: count += 1 DATA = remove_linked(x, y, DATA) print (count) try:input() except:break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s583891416
p00067
Accepted
# -*- coding: utf-8 -*- import sys import os def solve(island): visited = [] for lst in island: visit_row = [] for c in lst: if c == '1': visit_row.append(False) else: visit_row.append(True) visited.append(visit_row) def paintable(x, y): if 0 <= x < 12 and 0 <= y < 12 and not visited[x][y]: return True else: return False def paint(x, y, number): visited[x][y] = True island[x][y] = number if paintable(x-1, y): paint(x-1, y, number) if paintable(x+1, y): paint(x+1, y, number) if paintable(x, y-1): paint(x, y-1, number) if paintable(x, y+1): paint(x, y+1, number) paint_id = 2 for i in range(12): for j in range(12): if paintable(i, j): paint(i, j, paint_id) paint_id += 1 line = [] for lst in island: line += lst line = set(line) if '0' in lst: line.remove('0') print(len(line)) for s in sys.stdin: s = s.strip() if s != '': island = [list(s)] for i in range(11): s = input().strip() island.append(list(s)) solve(island) else: island = [] pass
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s420995704
p00067
Accepted
# -*- coding: utf-8 -*- """ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0067 """ import sys def solve(map): num_of_land = 0 work = [] lmap = map[:] for i in range(1, len(map)): for j in range(1, len(map[0])): if lmap[i][j] == 1: num_of_land += 1 work.append([i, j]) lmap[i][j] = 0 while work: y, x = work.pop() if lmap[y][x+1] == 1: # ???????????? work.append([y, x+1]) lmap[y][x+1] = 0 if lmap[y+1][x] == 1: # ???????????? work.append([y+1, x]) lmap[y+1][x] = 0 if lmap[y][x-1] == 1: # ???????????? work.append([y, x-1]) lmap[y][x-1] = 0 return num_of_land def main(args): data = [ [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0], [0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0], [0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0], [0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0], [0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0], [0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0], [0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] ] data = [] buff = [] for line in sys.stdin: line = line.strip() if line: if len(buff) == 0: buff = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] line = '0' + line + '0' buff.append([int(x) for x in line]) else: if len(buff) > 0: buff.append([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) data.append(buff) buff = [] if len(buff) > 0: buff.append([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) data.append(buff) buff = [] for map in data: result = solve(map) print(result) if __name__ == '__main__': main(sys.argv[1:])
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s855928469
p00067
Accepted
# -*- coding: utf-8 -*- """ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0067 """ import sys import copy def solve(map): num_of_land = 0 work = [] lmap = copy.deepcopy(map) for i, line in enumerate(lmap[1:], start=1): while 1 in line[1:-1]: j = line.index(1) num_of_land += 1 work.append([i, j]) lmap[i][j] = 0 while work: y, x = work.pop() if lmap[y][x+1] == 1: # ???????????? work.append([y, x+1]) lmap[y][x+1] = 0 if lmap[y+1][x] == 1: # ???????????? work.append([y+1, x]) lmap[y+1][x] = 0 if lmap[y][x-1] == 1: # ???????????? work.append([y, x-1]) lmap[y][x-1] = 0 return num_of_land def main(args): # data = [ # [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # [0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], # [0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0], # [0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0], # [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], # [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0], # [0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0], # [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], # [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0], # [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], # [0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0], # [0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0], # [0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], # [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], # # [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # [0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0], # [0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0], # [0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0], # [0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0], # [0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0], # [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0], # [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], # [0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0], # [0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], # [0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0], # [0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0], # [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] # ] data = [] buff = [] for line in sys.stdin: line = line.strip() if line: if len(buff) == 0: buff = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] line = '0' + line + '0' buff.append([int(x) for x in line]) else: if len(buff) > 0: buff.append([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) data.append(buff) buff = [] if len(buff) > 0: buff.append([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) data.append(buff) buff = [] for map in data: result = solve(map) print(result) if __name__ == '__main__': main(sys.argv[1:])
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s569880141
p00067
Accepted
import sys dx = [0, 0, -1, 1] dy = [1, -1, 0, 0] def main(): while True: map = [] count = 0 for i in range(12): map.append([x for x in input()]) for i in range(12): for j in range(12): if map[i][j] == '1': dfs(map, j, i) count += 1 print(count) if not sys.stdin.readline(): break def dfs(map, x, y): if x < 0 or y < 0 or x >= 12 or y >= 12: return map[y][x] = '0' for i in range(4): next_x = x + dx[i] next_y = y + dy[i] if next_x >= 0 and next_x < 12 and next_y >= 0 and next_y < 12: if map[next_y][next_x] == '1': dfs(map, next_x, next_y) if __name__ == '__main__': main()
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s043558012
p00067
Accepted
# Aizu Problem 0067: The Number of Islands # import sys, math, os # read input: PYDEV = os.environ.get('PYDEV') if PYDEV=="True": sys.stdin = open("sample-input.txt", "rt") class UndirectedGraph(): def __init__(self, grid): offsets = [[0, 1], [0, -1], [1, 0], [-1, 0]] N = len(grid) M = len(grid[0]) self.edges = {} self.nodes = [] for row in range(N): for col in range(M): if grid[row][col] == '1': self.AddNode((row, col)) for row_off, col_off in offsets: row2 = row + row_off col2 = col + col_off if 0 <= row2 < N and 0 <= col2 < M and grid[row2][col2] == '1': self.AddEdge((row, col), (row2, col2)) def AddNode(self, node): if node not in self.nodes: self.nodes.append(node) if node not in self.edges: self.edges[node] = [] def GetNodes(self): return self.nodes def GetEdges(self): return self.edges def AddEdge(self, node1, node2): self.AddNode(node1) self.AddNode(node2) if node1 not in self.edges[node2]: self.edges[node2].append(node1) if node2 not in self.edges[node1]: self.edges[node1].append(node2) def Degree(self, node): return len(self.edges[node]) def GetNeighbors(self, node): return self.edges.get(node, []) def GetPath(self, source, target): # find path by BFS: predecessor = {node: -1 for node in self.GetNodes()} predecessor[source] = 0 visited = {node: False for node in self.GetNodes()} queue = [source] while len(queue) > 0: current = queue.pop(0) visited[current] = True for neighbor in self.GetNeighbors(current): if not visited[neighbor] and neighbor not in queue: queue.append(neighbor) predecessor[neighbor] = current if neighbor == target: queue = [] break # build path from predecessor info: if predecessor[target] == -1: return [] path = [target] while path[0] != source: path = [predecessor[path[0]]] + path[:] return path def GetConnectedComponents(self): # identify connected components by BFS nodes = self.GetNodes()[:] connected_components = [] while len(nodes) > 0: queue = [nodes.pop()] component = [queue[0]] while len(queue) > 0: current = queue.pop(0) for neighbor in self.GetNeighbors(current): if neighbor in nodes and neighbor not in queue: queue.append(neighbor) nodes.remove(neighbor) component.append(neighbor) connected_components.append(component) return connected_components finished = False while True: grid = [] if finished: break while True: try: inp = input().strip() except EOFError: finished = True break if len(inp) == 0: break grid.append([_ for _ in inp]) G = UndirectedGraph(grid) print(len(G.GetConnectedComponents()))
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s177125481
p00067
Accepted
while True: c=0 islands=[] islandsID=[] ID=0 def dfs(row,col): global ID if 0<=row<12 and 0<=col<12: if islands[row][col]==1 and islandsID[row][col]==0: islandsID[row][col]=ID for dr, dc in [[0,1],[1,0],[0,-1],[-1,0]]: dfs(row+dr,col+dc) for i in range(12): islands.append(list(int(k) for k in input())) for _ in range(12): islandsID.append([0 for i in range(12)]) for i in range(12): for j in range(12): if islands[i][j]==1 and islandsID[i][j]==0: ID+=1 dfs(i,j) print(ID) try: c=input() except: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s738885388
p00067
Accepted
def paint(map_inp, x, y): if map_inp[y][x]: map_inp[y][x] = False if map_inp[y][x - 1]: map_inp = paint(map_inp, x - 1, y) if map_inp[y][x + 1]: map_inp = paint(map_inp, x + 1, y) if map_inp[y - 1][x]: map_inp = paint(map_inp, x, y - 1) if map_inp[y + 1][x]: map_inp = paint(map_inp, x, y + 1) return map_inp while True: try: m = [[False for i in range(14)] for j in range(14)] for i in range(12): inp = input() for j in range(12): if inp[j] == "1": m[i + 1][j + 1] = True cnt = 0 for i in range(12): for j in range(12): if m[i + 1][j + 1]: m = paint(m, j + 1, i + 1) cnt += 1 print(cnt) inp = input() except: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s200315613
p00067
Accepted
def get_input(): while True: try: yield ''.join(input()) except EOFError: break def nuri(table, m, x, y): if table[x-1][y] and m[x-1][y] == 0: m[x-1][y] = m[x][y] nuri(table, m, x-1, y) if table[x+1][y] and m[x+1][y] == 0: m[x+1][y] = m[x][y] nuri(table, m, x+1, y) if table[x][y-1] and m[x][y-1] == 0: m[x][y-1] = m[x][y] nuri(table, m, x, y-1) if table[x][y+1] and m[x][y+1] == 0: m[x][y+1] = m[x][y] nuri(table, m, x, y+1) return N = list(get_input()) for l in range(0,len(N),13): table = [[False for i in range(14)] for j in range(14)] for i in range(12): for j in range(12): if int(N[l+i][j]) == 1: table[i+1][j+1] = True m = [[0 for i in range(14)] for j in range(14)] cnt = 0 for i in range(1,13): for j in range(1,13): if table[i][j] and m[i][j] == 0: cnt += 1 m[i][j] = cnt nuri(table, m, i, j) print(cnt)
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s372930552
p00067
Accepted
while True: lst = [] for i in range(12): ls = [] s = input() for j in range(12): ls.append(int(s[j])) lst.append(ls) xd = [0,1,0,-1] yd = [1,0,-1,0] count = 0 def check(pt): if 11 < pt[0] or pt[0] < 0 or 11 < pt[1] or pt[1] < 0: return False elif(lst[pt[0]][pt[1]] == 1): lst[pt[0]][pt[1]] = 0 for i in range(4): check([pt[0]+xd[i],pt[1]+yd[i]]) return True else: return False def printMap(): for i in range(12): st = "" for j in range(12): st = st + str(lst[i][j]) print(st) print() for x in range(12): for y in range(12): if lst[x][y]: check([x,y]) count += 1 #printMap() print(count) try: input() except EOFError: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s113482097
p00067
Accepted
def counter():#島の数をカウントする count=0 #12*12個の値をひとつずつ調べる for x in range(len(is_data)): for y in range(len(is_data)): if is_data[x][y] == '1':#島が発見されたら dfs(x,y) count+=1 return count def dfs(x,y): if x<0 or x == len(is_data) or y<0 or y == len(is_data): return if is_data[x][y] == '1': is_data[x][y] = '0' dfs(x+1,y) dfs(x,y+1) dfs(x-1,y) dfs(x,y-1) is_data=[] while True: try: s = list(input())#入力 if not s:#空入力のときループぬける print(counter()) is_data=[] s = [] else: #ls = len(s) is_data.append(s) #for i in range(len(is_data)): #is_data[i] = list(map(int,is_data[i])) #is_data = np.array(is_data).reshape(ls,ls) #print(is_data) except EOFError: print(counter()) break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s229977533
p00067
Accepted
def counter():#島の数をカウントする count=0 #12*12個の値をひとつずつ調べる for x in range(len(is_data)): for y in range(len(is_data)): if is_data[x][y] == '1':#島が発見されたら dfs(x,y) count+=1 return count def dfs(x,y): if x<0 or x == len(is_data) or y<0 or y == len(is_data): return if is_data[x][y] == '1': is_data[x][y] = '0' dfs(x+1,y) dfs(x,y+1) dfs(x-1,y) dfs(x,y-1) is_data=[] while True: try: s = list(input())#入力 if not s:#空入力のとき print(counter()) is_data=[] s = [] else: is_data.append(s) except EOFError: print(counter()) break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s442033793
p00067
Accepted
direct = ((0, -1), (0, 1), (-1, 0), (1, 0)) #移動方向 def search(x, y, mp): if mp[x][y] == "1": #1を見つけたら0に書き換える mp[x][y] = "0" for dx, dy in direct: #4方向について繰り返す search(x + dx, y + dy, mp) while True: #mp...全体のマップ、上下左右を0で囲っておく mp = [list("0" + input() + "0") for _ in range(12)] mp.insert(0, [0] * 14) mp.append([0] * 14) ans = 0 for x in range(1, 13): for y in range(1, 13): if mp[x][y] == "1": ans += 1 search(x, y, mp) print(ans) try: input() except EOFError: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s824486759
p00067
Accepted
# AOJ 0067 The Number of Island # Python3 2018.6.16 bal4u # UNION-FIND library MAX = 12*12 id, size = [0]*MAX, [0]*MAX def init(n): for i in range(n): id[i], size[i] = i, 1 def root(i): while i != id[i]: id[i] = id[id[i]] i = id[i] return i def connected(p, q): return root(p) == root(q) def unite(p, q): i, j = root(p), root(q) if i == j: return if size[i] < size[j]: id[i] = j size[j] += size[i] else: id[j] = i size[i] += size[j] # UNION-FIND library arr = [[0 for r in range(12)] for c in range(12)] dr = [-1, 0, 1, 0] dc = [ 0, 1, 0,-1] while True: init(12*12) for r in range(12): arr[r] = list(map(int, input())) for r in range(12): for c in range(12): if arr[r][c] == 0: continue for i in range(4): nr, nc = r + dr[i], c + dc[i] if nr >= 0 and nr < 12 and nc >= 0 and nc < 12: if arr[nr][nc] == 1: unite(r*12+c, nr*12+nc) ans = 0 for r in range(12): for c in range(12): if arr[r][c] == 1 and root(r*12+c) == r*12+c: ans += 1 print(ans) try: input() except: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s581847963
p00067
Accepted
def dfs(board,y,x): if not board[y][x]: return 0 board[y][x]=0 dx,dy=[0,1,0,-1],[1,0,-1,0] for i in xrange(4): dfs(board,y+dy[i],x+dx[i]) return 1 while True: try: board=[[0]+map(int,raw_input()[:])+[0] for unused in xrange(12)] board.insert(13,[0]*14) board.insert(0,[0]*14) print sum([dfs(board,i/12+1,i%12+1) for i in xrange(12*12)]) raw_input() except: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s418594836
p00067
Accepted
import sys def solv(boad): boad = map(lambda x: [0] + x + [0], boad) boad.insert(0, [0] * 14) boad.append([0] * 14) n = sum([foo(boad, x, y) for x in xrange(1, 13) for y in xrange(1, 13)]) return n def foo(b, x, y): if b[y][x]: b[y][x] = 0 foo(b, x-1, y) foo(b, x+1, y) foo(b, x, y-1) foo(b, x, y+1) return 1 else: return 0 boads = [] b = [] for line in sys.stdin: line = line.rstrip() if line == '': boads.append(b) b = [] continue b.append(map(int, line)) boads.append(b) for boad in boads: print solv(boad)
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s971631115
p00067
Accepted
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import (division, absolute_import, print_function, unicode_literals) from sys import stdin def solve(L): n = 0 sets = set() for y in range(12): for x in range(12): if not L[y][x]: continue elif y and L[y-1][x]: L[y][x] = L[y-1][x] elif x and L[y][x-1]: L[y][x] = L[y][x-1] else: n += 1 sets.add(n) L[y][x] = n for x in range(10, -1, -1): if L[y][x] and L[y][x+1] and L[y][x] != L[y][x+1]: sets.discard(L[y][x]) L[y][x] = L[y][x+1] return len(sets) s = '\n' sep = '\n' while s: L = [] for i in range(12): L.append([int(s) for s in stdin.readline().rstrip()]) print(solve(L)) s = stdin.readline()
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s407051546
p00067
Accepted
class UnionFind(object): def __init__(self, size): self.data = [-1 for i in range(size)]; def unite(self, x, y): x = self.root(x); y = self.root(y); if (x != y): if (self.data[y] < self.data[x]): x, y = y, x; self.data[x] += self.data[y]; self.data[y] = x; return x != y; def same(self, x, y): return self.root(x) == self.root(y) def root(self, x): if self.data[x] < 0 : return x; self.data[x] = self.root(self.data[x]); return self.data[x]; def size(self, x): return -self.data[self.root(x)]; def pos(i, j): return i * 14 + j end_flag = False while True: uf = UnionFind(14 * 14); board = []; while True: try: s = raw_input(); except: end_flag = True break s = "0" + s.strip() + "0"; if (s == "00"): break; board.append(list(s)); sent = [0 for i in range(len(board[0]))] board = [sent] + board + [sent]; #print board island = 0 for i in range(1, len(board) - 1): for j in range(1, len(board[0]) - 1): if board[i][j] == '1': p = pos(i, j); f = False; if (board[1 + i][j] == '1'): uf.unite(p, pos(i + 1, j)); f = True; if (board[i - 1][j] == '1'): uf.unite(p, pos(i - 1, j)); f = True; if (board[i][j + 1] == '1'): uf.unite(p, pos(i, j + 1)); f = True; if (board[i][j - 1] == '1'): uf.unite(p, pos(i, j - 1)); f = True; if not f: island += 1 print island + len(filter(lambda x: x < -1, uf.data)); if end_flag: break;
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s264103602
p00067
Accepted
import sys A=range(12) m=[[0 for i in A] for j in A] def f(y,x): f=0 if m[y][x]==1: m[y][x]=c f=1 return f def LR(y,x,dx): P=[] x+=dx while 0<=x<12 and m[y][x]!=0: if f(y,x):P+=[(y,x)] x+=dx return P def UD(y,x,dy): P=[] y+=dy while 0<=y<12 and m[y][x]!=0: if f(y,x):P+=[(y,x)] y+=dy return P def v1(buf): f=0 while len(buf)>0: f=1 y,x=buf.pop() m[y][x]=c buf+=LR(y,x,-1)+LR(y,x,1)+UD(y,x,-1)+UD(y,x,1) return f def solve(): global c c=2 for i in A: x=m[i] while x.count(1)>0: v1([(i,x.index(1))]) c+=1 return c-2 y=0 for s in sys.stdin: if y<12: x=0 for e in map(int,list(s[:-1])): m[y][x]=e x+=1 y+=1 else: print solve() y=0 print solve()
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s619797258
p00067
Accepted
import sys A=range(12) m=[[0 for i in A] for j in A] def LRUD(y,x,d): P=[] x+=d[0] y+=d[1] if 0<=x<12 and 0<=y<12 and m[y][x]==1: m[y][x]=c P=[(y,x)] return P def v1(buf): while len(buf)>0: y,x=buf.pop() m[y][x]=c for e in [(-1,0),(1,0),(0,-1),(0,1)]: buf+=LRUD(y,x,e) return def solve(): global c c=2 for i in A: x=m[i] while x.count(1)>0: v1([(i,x.index(1))]) c+=1 print c-2 return 0 y=0 for s in sys.stdin: if y<12: x=0 for e in map(int,list(s[:-1])): m[y][x]=e x+=1 y+=1 else: y=solve() solve()
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s760017320
p00067
Accepted
import sys A=range(12) m=[[0 for i in A] for j in A] def f(y,x,d): P=[] x+=d[0] y+=d[1] if 0<=x<12 and 0<=y<12 and m[y][x]==1: m[y][x]=c P=[[y,x]] return P def v1(buf): global c while buf!=[]: y,x=buf.pop() m[y][x]=c for e in [(-1,0),(1,0),(0,-1),(0,1)]:buf+=f(y,x,e) c+=1 return def solve(): global c c=2 for i in A: while m[i].count(1)>0:v1([[i,m[i].index(1)]]) print c-2 return 0 y=0 for s in sys.stdin: if y<12: m[y]=map(int,list(s[:-1])) y+=1 else:y=solve() solve()
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s513991938
p00067
Accepted
import sys A=range(14) m=[[0 for i in A] for j in A] def f(y,x): if m[y][x]==1: m[y][x]=0 f(y,x+1) f(y,x-1) f(y+1,x) f(y-1,x) return def solve(): c=2 for i in A: while m[i].count(1)>0: f(i,m[i].index(1)) c+=1 print c-2 return 0 y=0 for s in sys.stdin: if y<12: m[y+1]=[0]+map(int,list(s[:-1]))+[0] y+=1 else:y=solve() solve()
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s924350196
p00067
Accepted
import sys A=range(14) m=[[0 for i in A]for j in A] def f(y,x): if m[y][x]==1: m[y][x]=0 f(y,x+1) f(y,x-1) f(y+1,x) f(y-1,x) return 1 def solve(): c=0 for i in A: while m[i].count(1)>0:c+=f(i,m[i].index(1)) print c return 0 y=0 for s in sys.stdin: if y<12: y+=1 m[y]=[0]+map(int,list(s[:-1]))+[0] else:y=solve() solve()
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s689042007
p00067
Accepted
l = 12 def island(f,x,y,m): rf = f[:][:] if rf[y][x] == 1: rf[y][x] = m for i in [-1,1]: if 0 <= x+i <= l-1: rf = island(rf,x+i,y,m) if 0 <= y+i <= l-1: rf = island(rf,x,y+i,m) return rf while True: f = [] m = 2 for i in range(l): f.append(map(int, list(raw_input()))) for y in range(l): for x in range(l): f = island(f,x,y,m) m += 1 ans = [] for i in range(l): for j in range(l): ans.append(f[i][j]) ans = list(set(ans)) if ans.count(0) == 1: print len(ans) - 1 else: print len(ans) try : raw_input() except: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s782303714
p00067
Accepted
def island(f,x,y,m): rf = f[:][:] rf[y][x] = m for i in [-1,1]: if 0 <= x+i <= 11 and rf[y][x+i] == 1: rf = island(rf,x+i,y,m) if 0 <= y+i <= 11 and rf[y+i][x] == 1: rf = island(rf,x,y+i,m) return rf while True: f = []; m = 2 for i in range(12): f.append(map(int, list(raw_input()))) for y in range(12): for x in range(12): if f[y][x] == 1: f = island(f,x,y,m) m += 1 ans = [] for i in range(12): for j in range(12): ans.append(f[i][j]) ans = list(set(ans)) print len(ans) - 1*ans.count(0) try:raw_input() except: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s118536302
p00067
Accepted
dx, dy = [-1, 0, 1, 0], [0, -1, 0, 1] while True: ans = 0 try: m = ['0'*14] + ['0'+raw_input()+'0' for i in xrange(12)] + ['0'*14] f = [[False for i in xrange(14)] for j in xrange(14)] for y in xrange(1, 13): for x in xrange(1, 13): if m[y][x]=='1' and f[y][x]==False: q = [[x, y]] f[y][x] = True while len(q)>0: px, py = q.pop(0) for k in xrange(4): nx = px + dx[k]; ny = py + dy[k]; if m[ny][nx]=='1' and f[ny][nx]==False: f[ny][nx] = True q.append([nx, ny]) ans += 1 print ans raw_input() except EOFError: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s373305269
p00067
Accepted
W,H = 12,12 dxy = [[1,0],[0,1],[-1,0],[0,-1]] def solve(field,w,h): if 0 <= w < W and 0 <= h < H and field[h][w] == "1": field[h][w] = "0" for dx,dy in dxy: solve(field,w+dx,h+dy) while 1: try: field = [list(raw_input()) for i in range(H)] ans = 0 for h in range(H): for w in range(W): if field[h][w] == "1": solve(field,w,h) ans += 1 print ans raw_input() except: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s877356232
p00067
Accepted
def get_map(): map = [] while True: try: tmp = list(raw_input()) if len(tmp) != 12: break map.append(tmp) except: break return map def remove_island(x, y, map): map[x][y] = 0 move = [[1, 0], [0, 1], [-1, 0], [0, -1]] for i, j in move: if 0 <= x + i <= 11 and 0 <= y + j <= 11 and map[x + i][y + j] == '1': map = remove_island(x + i, y + j, map) return map[:] while True: map = get_map() if len(map) != 12: break count = 0 for x in range(12): for y in range(12): if map[x][y] == '1': count += 1 map = remove_island(x, y, map) print count
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s564769940
p00067
Accepted
H, W = 12, 12 dx = [0, 0, -1, 1] dy = [-1, 1, 0, 0] def dfs(i, j): field[i][j] = 0 for k in xrange(4): ny = i + dy[k] nx = j + dx[k] if nx < 0 or ny < 0 or nx >= W or ny >= H: continue if field[ny][nx] == 1: dfs(ny, nx) while 1: field = [] for i in xrange(H): field.append(map(int, raw_input())) ret = 0 for i in xrange(H): for j in xrange(W): if field[i][j] == 1: dfs(i, j) ret += 1 print ret try: raw_input() except EOFError: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s719853437
p00067
Accepted
def count_island(ban): def remove(x, y): if 0 <= y < 12 and 0 <= x < 12 and ban[y][x] == 1: ban[y][x] = 0 for dx, dy in [(-1,0), (1,0), (0,-1), (0,1)]: remove(x + dx, y + dy) count = 0 for y in range(12): for x in range(12): if ban[y][x] == 1: count += 1 remove(x, y) print(count) ban = [] while True: try: s = input() except EOFError: break if s: ban.append(list(map(int, s))) else: count_island(ban) ban = [] if ban: count_island(ban)
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s744503109
p00067
Accepted
def dfs(L,i,j): if L[i][j] > 0: L[i][j] = 0 else: return for k in [-1, 1]: ni = i + k if ni not in range(12): continue if L[ni][j] > 0: dfs(L, ni, j) for k in [-1, 1]: nj = j + k if nj not in range(12): continue if L[i][nj] > 0: dfs(L, i, nj) return while True: L = [] try: for _ in range(12): L.append( [int(x) for x in input()] ) except: break c = 0 for i in range(12): for j in range(12): if L[i][j] > 0: c += 1 dfs(L, i, j) print(c) try: input() except: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s508537858
p00067
Accepted
dx=[1,0,-1,0] dy=[0,1,0,-1] def dfs(i,j,m): m[i][j] = 0 for k in range(4): x = i + dx[k] y = j + dy[k] if (x < 12 and x >= 0 and y < 12 and y >= 0 and m[x][y] == 1): dfs(x,y,m) return while True: try: m = [] sum = 0 for i in range(12): m.append(list(map(int, input()))) for i in range(12): for j in range(12): if (m[i][j] == 1): dfs(i,j,m) sum += 1 print(sum) input() except: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s515573817
p00067
Accepted
def dfs(A,x,y): X = [1,0,-1,0] Y = [0,1,0,-1] A[x][y] = 0 for i in range(4): xx = x + X[i] yy = y + Y[i] if 0 <= xx < 12 and 0 <= yy < 12: if A[xx][yy] == "1": dfs(A,xx,yy) if __name__ == '__main__': A = [] while True: try: for _ in range(12): ll = list(input()) A.append(ll) iland = 0 for x in range(12): for y in range(12): if A[x][y] == "1": iland += 1 dfs(A,x,y) print(iland) s = input() A=[] except EOFError: break
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s498501577
p00067
Accepted
import sys dataset = [] lst = [] for line in sys.stdin: lst.append(line) minilist = [] for i in range(0, len(lst)): if lst[i] == '\n': dataset.append(minilist) minilist = [] else: minilist.append(lst[i]) dataset.append(minilist) for i in range(0, len(dataset)): memo = [] for j in range(0, 12): lst = [] for k in range(0, 12): lst.append(True) memo.append(lst) cnt = 0 for j in range(0, 12): for k in range(0, 12): if dataset[i][j][k] == '1' and memo[j][k]: queue = [[j, k]] while queue != []: now = queue.pop(0) if memo[now[0]][now[1]] and dataset[i][now[0]][now[1]]=='1': memo[now[0]][now[1]] = False for p in range(0, 2): nexth = now[0]-1+2*p nextw = now[1]-1+2*p if 0<=nexth and nexth<12: queue.append([nexth, now[1]]) if 0<=nextw and nextw<12: queue.append([now[0], nextw]) cnt += 1 print cnt
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>
s084866369
p00067
Accepted
state = 1 while(state): a = [[0 for i in range(12)] for j in range(12)] for i in range(12): a[i] = [int(i) for i in list(input())] try: _ = input() except EOFError: state = 0 def erase(y,x): if a[y][x] != 0: a[y][x] = 0 if x-1 > -1: erase(y,x-1) if x+1 < 12: erase(y,x+1) if y-1 > -1: erase(y-1,x) if y+1 < 12: erase(y+1,x) count = 0 for i in range(12): for j in range(12): if a[i][j] != 0: count += 1 erase(i,j) print(count)
111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001
5 13 4
<H1>島の数</H1> <p> 地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。 </p> <pre> ■■■■□□□□■■■■ ■■■□□□□□■■■■ ■■□□□□□□■■■■ ■□□□□□□□■■■■ □□□■□□□■□□□□ □□□□□□■■■□□□ □□□□□■■■■■□□ ■□□□■■■■■■■□ ■■□□□■■■■■□□ ■■■□□□■■■□□□ ■■■■□□□■□□□□ □□□□□□□□□□□□ </pre> <p> マスのデータを読み込んで、島の数を出力するプログラムを作成してください。 </p> <H2>Input</H2> <p> 入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。 </p> <p> データセットの数は 20 を超えません。 </p> <H2>Output</H2> <p> データセットごとに、島の数を1行に出力します。 </p> <H2>Sample Input</H2> <pre> 111100001111 111000001111 110000001111 100000001111 000100010000 000000111000 000001111100 100011111110 110001111100 111000111000 111100010000 000000000000 010001111100 110010000010 010010000001 010000000001 010000000110 010000111000 010000000100 010000000010 010000000001 010010000001 010010000010 111001111100 000000000000 111111111111 100010100001 100010100001 100010100001 100010100001 100100100101 101000011101 100000000001 100000000001 111111111111 100000000001 </pre> <H2>Output for the Sample Input</H2> <pre> 5 13 4 </pre> <H2>Hint</H2> <p> 以下はサンプルインプットを■と□で表したものです。 <pre> ■■■■□□□□■■■■  □■□□□■■■■■□□  □□□□□□□□□□□□ ■■■□□□□□■■■■  ■■□□■□□□□□■□  ■■■■■■■■■■■■ ■■□□□□□□■■■■  □■□□■□□□□□□■  ■□□□■□■□□□□■ ■□□□□□□□■■■■  □■□□□□□□□□□■  ■□□□■□■□□□□■ □□□■□□□■□□□□  □■□□□□□□□■■□  ■□□□■□■□□□□■ □□□□□□■■■□□□  □■□□□□■■■□□□  ■□□□■□■□□□□■ □□□□□■■■■■□□  □■□□□□□□□■□□  ■□□■□□■□□■□■ ■□□□■■■■■■■□  □■□□□□□□□□■□  ■□■□□□□■■■□■ ■■□□□■■■■■□□  □■□□□□□□□□□■  ■□□□□□□□□□□■ ■■■□□□■■■□□□  □■□□■□□□□□□■  ■□□□□□□□□□□■ ■■■■□□□■□□□□  □■□□■□□□□□■□  ■■■■■■■■■■■■ □□□□□□□□□□□□  ■■■□□■■■■■□□  ■□□□□□□□□□□■ </pre> </p>