HooLee
If you believe, you can!
C++博客
首頁
新隨筆
新文章
聯系
管理
poj1021_2D-Nim
題意:對比連個圖是否相同。相同的條件是A圖中每個連通的區域都在B圖中有一塊相同的區域與之對應。經過旋轉后對應也可以。
解題思路:
思路一:暴力式。遍歷并存儲A圖中所有聯通的區域,然后在B中逐個尋找與之對應的圖形。
思路二:等價轉化式。比較兩個圖中對應點的“連通度”是否相同。相同輸出YES。空白點的連通度為0,空白點的連通度為它所在行和列與之相連的非空白點的個數。
方式二非常巧妙,將二維問題轉化為了一維問題,大大簡化了問題。
代碼
1
import
java.io.
*
;
2
import
java.util.
*
;
3
class
Main
4
{
5
private
static
int
[][] map1
=
new
int
[
110
][
110
];
6
private
static
int
[][] map2
=
new
int
[
110
][
110
];
7
private
static
CountNode[] ctnd1
=
new
CountNode[
110
*
110
];
8
private
static
CountNode[] ctnd2
=
new
CountNode[
110
*
110
];
9
private
static
int
col;
10
private
static
int
row;
11
public
static
void
main(String[] args)
12
{
13
14
Scanner sc
=
new
Scanner(System.in);
15
int
t
=
sc.nextInt();
16
for
(
int
ii
=
0
; ii
<
t; ii
++
)
17
{
18
for
(
int
j
=
0
; j
<
map1.length; j
++
)
19
{
20
Arrays.fill(map1[j],
0
);
21
Arrays.fill(map2[j],
0
);
22
}
23
Arrays.fill(ctnd1,
new
CountNode(
0
));
24
Arrays.fill(ctnd2,
new
CountNode(
0
));
25
26
col
=
sc.nextInt();
27
row
=
sc.nextInt();
28
int
n
=
sc.nextInt();
29
int
thiscol, thisrow;
30
//
31
//
System.out.println("col=" + col + " row=" + row + " n=" + n);
32
//
33
for
(
int
k
=
0
; k
<
n; k
++
)
//
read map1[][]
34
{
35
thiscol
=
sc.nextInt();
36
thisrow
=
sc.nextInt();
37
38
map1[thisrow][thiscol]
=
1
;
39
}
40
for
(
int
k
=
0
; k
<
n; k
++
)
//
read map2[][]
41
{
42
thiscol
=
sc.nextInt();
43
thisrow
=
sc.nextInt();
44
45
map2[thisrow][thiscol]
=
1
;
46
}
47
//
48
/**/
/*
System.out.println("map1[][]");
49
for(int i = 0; i < row; i++)
50
{
51
for(int j = 0; j < col; j++)
52
System.out.print(map1[i][j]);
53
System.out.println();
54
}
55
System.out.println("map2[][]");
56
for(int i = 0; i < row; i++)
57
{
58
for(int j = 0; j < col; j++)
59
System.out.print(map2[i][j]);
60
System.out.println();
61
}
*/
62
//
63
getAllCounts();
64
Arrays.sort(ctnd1);
65
Arrays.sort(ctnd2);
66
boolean
flag
=
true
;
67
for
(
int
i
=
0
; i
<
ctnd1.length; i
++
)
68
{
69
if
(ctnd1[i].getCount()
!=
ctnd2[i].getCount())
{
70
flag
=
false
;
71
System.out.println(
"
NO
"
);
72
break
;
73
}
74
}
75
if
(flag)
76
System.out.println(
"
YES
"
);
77
}
78
79
}
80
private
static
void
getAllCounts()
81
{
82
int
p
=
0
;
83
for
(
int
i
=
0
; i
<
row; i
++
)
84
{
85
for
(
int
j
=
0
; j
<
col; j
++
)
86
{
87
if
(map1[i][j]
==
1
)
88
ctnd1[p
++
]
=
new
CountNode(getThisMap1Counts(i, j));
89
if
(map2[i][j]
==
1
)
90
ctnd2[p
++
]
=
new
CountNode(getThisMap2Counts(i, j));
91
}
92
}
93
}
94
private
static
int
getThisMap1Counts(
int
r,
int
c)
//
map1[][]的連通度
95
{
96
int
count
=
0
;
97
for
(
int
i
=
r
-
1
; i
>=
0
&&
map1[i][c]
==
1
; i
--
)
98
count
++
;
99
for
(
int
i
=
r
+
1
; i
<
row
&&
map1[i][c]
==
1
; i
++
)
100
count
++
;
101
for
(
int
i
=
c
-
1
; i
>=
0
&&
map1[r][i]
==
1
; i
--
)
102
count
++
;
103
for
(
int
i
=
c
+
1
; i
<
col
&&
map1[r][i]
==
1
; i
++
)
104
count
++
;
105
return
count
+
1
;
106
}
107
private
static
int
getThisMap2Counts(
int
r,
int
c)
//
map2[][]的連通度
108
{
109
int
count
=
0
;
110
for
(
int
i
=
r
-
1
; i
>=
0
&&
map2[i][c]
==
1
; i
--
)
111
count
++
;
112
for
(
int
i
=
r
+
1
; i
<
row
&&
map2[i][c]
==
1
; i
++
)
113
count
++
;
114
for
(
int
i
=
c
-
1
; i
>=
0
&&
map2[r][i]
==
1
; i
--
)
115
count
++
;
116
for
(
int
i
=
c
+
1
; i
<
col
&&
map2[r][i]
==
1
; i
++
)
117
count
++
;
118
return
count
+
1
;
119
}
120
121
122
}
123
class
CountNode
implements
Comparable
<
CountNode
>
124
{
125
private
int
count;
126
public
CountNode(
int
count)
127
{
128
this
.count
=
count;
129
}
130
public
int
getCount()
131
{
132
return
count;
133
}
134
public
int
compareTo(CountNode n2)
135
{
136
return
n2.count
-
this
.count;
137
}
138
}
139
posted on 2013-04-14 20:10
小鼠標
閱讀(509)
評論(0)
編輯
收藏
引用
所屬分類:
Java基礎練習
只有注冊用戶
登錄
后才能發表評論。
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
相關文章:
編輯距離
閏年判斷
正則表達式簡單筆記
Excel格式地址轉換
一道模擬題——機器人行走距離計算
排列練習2
素數篩法
排列組合練習
排列組合
poj1068Parencodings
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
Copyright ©2025 小鼠標 Powered by:
博客園
模板提供:
滬江博客
<
2012年8月
>
日
一
二
三
四
五
六
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
常用鏈接
我的隨筆
我的評論
我參與的隨筆
隨筆分類
(111)
C語言(3)
DP(9)
Java筆記(1)
Java基礎練習(25)
安卓(1)
本科畢設(1)
博弈(1)
大數(7)
回溯(2)
排序(10)
暑期培訓周賽(3)
數據結構(7)
數論(1)
水題(8)
圖論(24)
網選訓練(8)
隨筆檔案
(127)
2014年3月 (1)
2013年7月 (10)
2013年5月 (1)
2013年4月 (11)
2013年3月 (8)
2012年10月 (1)
2012年9月 (12)
2012年8月 (38)
2012年7月 (14)
2012年6月 (2)
2012年5月 (8)
2012年4月 (6)
2012年3月 (6)
2012年2月 (4)
2011年8月 (5)
friends
陳鋼
大鵬
黨姐
焦林楓
汪濤
小白學長
媛姐
媛姐csdn
最新評論
1.?re: 線段樹
是這個樣子的,所以在OJ有時候“卡住”了也不要太灰心,沒準真的不是自己的原因呢。
加油,祝你好運啦!
--小鼠標
2.?re: 線段樹
對于編程競賽來說,Java所需時間一般為C/C++的兩倍。合理的競賽給Java的時間限制是給C/C++的兩倍。
--傷心的筆
3.?re: poj1273--網絡流
過來看看你。
--achiberx
4.?re: (轉)ubuntu11.10無法啟動無線網絡的解決方法
膜拜大神。。查了一個下午資料終于在這里解決了問題。。神牛說的區域賽難道是ACM區域賽。。?
--Hang
5.?re: 快速排序、線性時間選擇
博主,謝謝你的文章。你的方法可以很好的處理分區基準在數組中重復的情況,書上的方法遇到這種輸入會堆棧溢出。書上給出了解釋但給的方法貌似不簡潔。
--lsxqw2004
閱讀排行榜
1.?單調隊列(5497)
2.?Linux select()函數使用(3990)
3.?快速排序、線性時間選擇(3719)
4.?poj3468--絕對經典的線段樹題(3642)
5.?優先隊列--堆實現(3309)
亚洲成色www久久网站夜月
|
久久青青草原精品国产不卡
|
亚洲AV日韩AV天堂久久
|
伊人久久大香线焦AV综合影院
|
久久精品亚洲福利
|
无夜精品久久久久久
|
亚洲AV无码久久精品色欲
|
99久久综合狠狠综合久久
|
久久婷婷国产剧情内射白浆
|
青青青青久久精品国产h
|
一级做a爰片久久毛片毛片
|
精品精品国产自在久久高清
|
久久精品人人做人人爽电影
|
97久久精品人人做人人爽
|
囯产极品美女高潮无套久久久
|
国产精品一区二区久久精品无码
|
老男人久久青草av高清
|
国产—久久香蕉国产线看观看
|
久久亚洲中文字幕精品有坂深雪
|
四虎国产精品免费久久5151
|
性欧美大战久久久久久久久
|
一个色综合久久
|
精品久久久久久国产牛牛app
|
久久这里只有精品18
|
久久大香萑太香蕉av
|
午夜肉伦伦影院久久精品免费看国产一区二区三区
|
久久亚洲AV成人出白浆无码国产
|
久久精品女人天堂AV麻
|
91精品国产9l久久久久
|
久久综合九色综合网站
|
久久久久亚洲av成人网人人软件
|
久久激情亚洲精品无码?V
|
99久久成人18免费网站
|
久久er热视频在这里精品
|
久久99热精品
|
国产精品久久久天天影视香蕉
|
国产精品岛国久久久久
|
久久亚洲国产欧洲精品一
|
26uuu久久五月天
|
久久久久亚洲精品天堂久久久久久
|
精品综合久久久久久98
|