ZOJ1002_FireNet

ZOJ1002 - FireNet

据说是8皇后问题的变种,但我表示不知道什么是8皇后问题。
 
我自己的话有两种想法,一种是我觉得一定能AC的穷举法,找出所有情况,找到最大值;第二种是我不肯定能AC的,方法是这样:先找能攻击范围最小的安防点,然后依次类往上放,放到没有空角为止。
 
我还没开始写,我打算先用第二种方法试试,等我做完了再更新相关内容。这道题目做完我也会去看下8皇后问题和01背包问题,然后熟悉完之后写个笔记什么的。
 
my_1
  1 // 我这里只记录了一次power值,但是我觉得应该要不停的刷新power,而且power值不能算上别人的已放的射击范围。
  2 // 为了要硬用上链表,我的逻辑乱了很多,中间过程也冗余了很多。
  3 // 其实用数组完全够了,而且很简单。
  4 // 中间关于删除中间项,其实可以用双向链表,循环次数会减少。
  5 
  6 //
  7 //单独设置一个头结点 和 未单独设置一个头结点是有区别的!!!!
  8 //单独设置头结点 可以用在void 函数中 用 *head->???来更新
  9 //而未单独设置头结点的 需要改变head地址,所以要不在void中传**  要不就在返回值返回新head
 10 //
 11 
 12 
 13 
 14 #include <stdio.h>
 15 #include <stdlib.h>
 16 
 17 #define kSquareStateWall                1
 18 #define kSquareStateCastle            2
 19 #define kSquareStateUnderFire    3
 20 #define kSquareStateEmpty            4
 21 
 22 struct squareLeft_node {
 23    int row;
 24    int column;
 25    int power;
 26    struct squareLeft_node *next;
 27 };
 28 
 29 struct squareLeft_node *createSquareLeft(int city[4][4], int size);
 30 struct squareLeft_node *pickTheWeakestNode(struct squareLeft_node *leftS);
 31 int getNodePower(int row, int column, int city[4][4], int size);
 32 struct squareLeft_node *updateCityStateAndSquareLeftByWeakestNode(struct squareLeft_node *weakest, int city[4][4],struct squareLeft_node *leftS, int size);
 33 void resetCityState(int city[4][4]);
 34 
 35 // private
 36 struct squareLeft_node *deleteNode(struct squareLeft_node *head, int row, int column);
 37 
 38 int main()
 39 {
 40    int sizeOfCity = 0;    
 41    char squareType;
 42    int cityState[4][4] = {0};
 43    int row,column;
 44    int maxBlockhouse = 0;
 45 
 46    struct squareLeft_node *squareLeftHead;
 47    struct squareLeft_node *weakestNode;
 48 
 49    while (scanf("%d", &sizeOfCity) != EOF)
 50    {
 51        if (0 == sizeOfCity)
 52            return 0;
 53        //----------------------Input-----------------------------
 54        getchar();
 55        for (row = 0; row < sizeOfCity; ++row)
 56        {
 57            for (column=0; column < sizeOfCity; ++column)
 58            {
 59                scanf("%c", &squareType);
 60                if ('.' == squareType)
 61                {
 62                    cityState[row][column] = kSquareStateEmpty;
 63                } else if ('X' == squareType)
 64                {
 65                    cityState[row][column] = kSquareStateWall;
 66                }
 67            }
 68            getchar();
 69        }
 70 
 71        //---------------------Find max------------------------
 72        squareLeftHead = createSquareLeft(cityState, sizeOfCity);
 73        weakestNode = squareLeftHead;
 74 
 75        while (NULL != squareLeftHead)
 76        {
 77            weakestNode = pickTheWeakestNode(squareLeftHead);
 78            squareLeftHead = updateCityStateAndSquareLeftByWeakestNode(weakestNode, cityState, squareLeftHead,sizeOfCity);
 79            maxBlockhouse++;
 80        }
 81 
 82 
 83        //----------------------Output--------------------------
 84        printf("%d\n", maxBlockhouse);
 85 
 86        //---------------------ResetCity-------------------------
 87        resetCityState(cityState);
 88        maxBlockhouse = 0;
 89        sizeOfCity = 0;
 90    }
 91    return 0;
 92 }
 93 
 94 
 95 struct squareLeft_node *createSquareLeft(int city[4][4], int size)
 96 {
 97    struct squareLeft_node *head, *tail, *tmp;
 98    int row,column;
 99 
100    head = tail = NULL;
101    if (0 == city[0][0])
102    {
103        return NULL;
104    }
105 
106    for (row = 0; row < size; ++row)
107    {
108        for (column = 0; column < size; ++column)
109        {
110            if (0 == city[row][column])
111            {
112                row = 4;
113                column = 4;
114                return head;
115            }
116            if ( (kSquareStateWall == city[row][column]) || 
117                (kSquareStateUnderFire == city[row][column]) || 
118                (kSquareStateCastle == city[row][column]) )
119            {
120                continue;
121            }
122            tmp = (struct squareLeft_node*)malloc(sizeof(struct squareLeft_node));
123            tmp->column = column;
124            tmp->row = row;
125            tmp->power = getNodePower(row, column, city, size);
126            tmp->next = NULL;
127 
128            if (NULL == head)
129            {
130                // For the first time 
131                head = tmp;
132            } else    
133            {
134                // Insert the new node
135                tail->next = tmp;
136            }
137            // update tail
138            tail = tmp;
139        }
140    }
141 
142    return head;
143 }
144 
145 struct squareLeft_node *pickTheWeakestNode(struct squareLeft_node *leftS)
146 {
147    struct squareLeft_node *weakest, *tmp;
148    weakest = leftS;
149    tmp = leftS;
150 
151    while (tmp != NULL)
152    {
153        if (weakest->power > tmp->power)
154        {
155            weakest = tmp;
156        }
157        tmp = tmp->next;
158    }
159    return weakest;
160 }
161 
162 int getNodePower(int row, int column, int city[4][4], int size)
163 {
164    int power = 0;
165    int r,c;
166 
167    // power upward
168    if (0 != row)
169    {
170        for (r = row-1; r >= 0; --r)
171        {
172            if (kSquareStateWall != city[r][column])
173                power++;
174            else
175                break;
176        }
177    }
178    // power downward
179    if (size != row)
180    {
181        for (r = row+1; r < size; ++r)
182        {
183            if (kSquareStateWall != city[r][column])
184                power++;
185            else
186                break;
187        }
188    }
189    // power leftward
190    if (0 != column)
191    {
192        for (c = column-1; c >= 0; --c)
193        {
194            if (kSquareStateWall != city[row][c])
195                power++;
196            else
197                break;
198        }
199    }
200    // power rightward
201    if (size != column)
202    {
203        for (c = column+1; c < size; ++c)
204        {
205            if (kSquareStateWall != city[row][c])
206                power++;
207            else
208                break;
209        }
210    }
211 
212    return power;
213 }
214 
215 struct squareLeft_node *updateCityStateAndSquareLeftByWeakestNode(struct squareLeft_node *weakest, int city[4][4],struct squareLeft_node *leftS, int size)
216 {
217    int row = weakest->row;
218    int column = weakest->column;
219    struct squareLeft_node *newHead = leftS;
220    int r,c;
221 
222    // power upward
223    if (0 != row)
224    {
225        for (r = row-1; r >= 0; --r)
226        {
227            if (kSquareStateWall != city[r][column])
228            {
229                city[r][column] = kSquareStateUnderFire;
230                leftS = deleteNode(leftS, r, column);
231            }
232            else
233                break;
234        }
235    }
236    // power downward
237    if (size != row)
238    {
239        for (r = row+1; r < size; ++r)
240        {
241            if (kSquareStateWall != city[r][column])
242            {
243                city[r][column] = kSquareStateUnderFire;
244                leftS = deleteNode(leftS, r, column);
245            }
246            else
247                break;
248        }
249    }
250    // power leftward
251    if (0 != column)
252    {
253        for (c = column-1; c >= 0; --c)
254        {
255            if (kSquareStateWall != city[row][c])
256            {
257                city[row][c] = kSquareStateUnderFire;
258                leftS = deleteNode(leftS, row, c);
259            }
260            else
261                break;
262        }
263    }
264    // power rightward
265    if (size != column)
266    {
267        for (c = column+1; c < size; ++c)
268        {
269            if (kSquareStateWall != city[row][c])
270            {
271                city[row][c] = kSquareStateUnderFire;
272                leftS = deleteNode(leftS, row, c);
273            }
274            else
275                break;
276        }
277    }
278    city[row][column] = kSquareStateCastle;
279    newHead = deleteNode(leftS, row, column);
280 
281    return newHead;
282 }
283 
284 void resetCityState(int city[4][4])
285 {
286    int i,j;
287    for (i = 0; i<4; ++i)
288    {
289        for (j = 0; j < 4; ++j)
290        {
291            city[i][j] = 0;
292        }
293    }
294 }
295 
296 struct squareLeft_node *deleteNode(struct squareLeft_node *head, int row, int column)
297 {
298    struct squareLeft_node *tmp = head;
299    struct squareLeft_node *preNode = NULL;
300 
301    preNode = tmp;
302    while (NULL != tmp)
303    {
304        if ( (tmp->row == row) && (tmp->column == column))
305        {
306            break;
307        }
308        preNode = tmp;
309        tmp = tmp->next;
310    }
311 
312    if (NULL == tmp)
313    {
314        return head;
315    }
316 
317    if (head == tmp)
318    {
319        head = tmp->next;
320        free(tmp);
321    } else
322    {
323        preNode->next = tmp->next;
324        free(tmp);
325    }
326 
327    return head;
328 }

 

转载于:https://www.cnblogs.com/guangTouT/archive/2012/11/25/2787186.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值