2021-09-29每日刷题打卡

一、POJ-2044:Weather Forecast

1.1 问题描述

You are the God of Wind.
By moving a big cloud around, you can decide the weather: it invariably rains under the cloud, and the sun shines everywhere else.

But you are a benign God: your goal is to give enough rain to every field in the countryside, and sun to markets and festivals. Small humans, in their poor vocabulary, only describe this as “weather forecast”.

You are in charge of a small country, called Paccimc. This country is constituted of 4 x 4 square areas, denoted by their numbers.

img

Your cloud is of size 2 x 2, and may not cross the borders of the country.

You are given the schedule of markets and festivals in each area for a period of time.

你会得到每个地区在一段时间内的市场和节庆的时间表。

On the first day of the period, it is raining in the central areas (6-7-10-11), independently of the schedule.

On each of the following days, you may move your cloud by 1 or 2 squares in one of the four cardinal directions (North, West, South, and East), or leave it in the same position. Diagonal moves are not allowed. All moves occur at the beginning of the day.

You should not leave an area without rain for a full week (that is, you are allowed at most 6 consecutive days without rain). You don’t have to care about rain on days outside the period you were given: i.e. you can assume it rains on the whole country the day before the period, and the day after it finishes.

题目大意:

有一个城镇,是44的大小的,然后你控制一块云彩,22的,你每天可以有9种走的方法,上下左右,或者不动,走的时候可以走1或者2步,云彩所在的地方肯定会下雨,然后给你最多365天的安排,要求某些日子的某些城镇不能下雨(因为有节假日),还有任何地方都不能有连续超过6天不下雨。

1.2 问题解决

#include<cstdio>
#include<cstring>

int DAY[367];
bool mark[366][10][8][8][8][8];
int ys[12] = {0 ,1 ,2 ,3 ,0 ,4 ,5 ,6 ,0 ,7 ,8 ,9};
int dir[9][2] = {0 ,0 ,0 ,1 ,0 ,-1 ,1 ,0 ,-1 ,0 ,0 ,2 ,0 ,-2 ,2 ,0 ,-2 ,0};
int OK ,T;


bool ok(int t ,int x ,int y ,int a[])
{


    int aa = (x - 1) * 4 + y;
    int bb= (x - 1 + 1) * 4 + y;
    int cc = (x - 1) * 4 + y + 1;
    int dd = (x - 1 + 1) * 4 + y + 1;
    if(x < 1 || x > 3 || y < 1 || y > 3)
    return 0;
    if(a[1] >= 7 || a[2] >= 7 || a[3] >= 7 || a[4] >= 7)
    return 0;
    if(((1 << aa) & DAY[t]) || ((1 << bb) & DAY[t]) || ((1 << cc) & DAY[t]) || ((1 << dd) & DAY[t]))
    return 0;
    if(mark[t][ys[aa]][a[1]][a[2]][a[3]][a[4]]) return 0;
    return 1;


}


void DFS(int t ,int x ,int y ,int a[])
{
    if(t == T) OK = 1;
    if(OK) return ;
    int b[5];
    for(int i = 0 ;i < 9 ;i ++)
    {
        if(t == 0 && i) break;
        int nowt = t + 1;
        int nowx = x + dir[i][0];
        int nowy = y + dir[i][1];
        for(int j = 1 ;j <= 4 ;j ++)
        b[j] = a[j] + 1;
        if(nowx == 1 && nowy == 1) b[1] = 0;
        if(nowx == 1 && nowy == 3) b[2] = 0;
        if(nowx == 3 && nowy == 1) b[3] = 0;
        if(nowx == 3 && nowy == 3) b[4] = 0;
        if(ok(nowt ,nowx ,nowy ,b))
        {
            mark[nowt][ys[(nowx-1)*4+nowy]][b[1]][b[2]][b[3]][b[4]] = 1;
            DFS(nowt ,nowx ,nowy ,b);
        }
    }
    return ;
}


int main ()
{
    int i ,j;
    while(~scanf("%d" ,&T) && T)
    {
        memset(DAY ,0 ,sizeof(DAY));
        for(i = 1 ;i <= T ;i ++)
        {
            int tmp;
            for(j = 1 ;j <= 16 ;j ++)
            {
                scanf("%d" ,&tmp);
                DAY[i] = DAY[i] | (tmp << j);
            }
        }
        memset(mark ,0 ,sizeof(mark));
        OK = 0;
        int a[5];
        a[1] = a[2] = a[3] = a[4] = 0;
        DFS(0 ,2 ,2 ,a);
        printf("%d\n" ,OK);
    }
    return 0;
}

二、Leetcode-148:排序链表

2.1 问题描述

img

2.2 问题解决

class Solution {
public:
    ListNode* sortList(ListNode* head) {
        return sortList(head, nullptr);
    }

    ListNode* sortList(ListNode* head, ListNode* tail) {
        if (head == nullptr) {
            return head;
        }
        if (head->next == tail) {
            head->next = nullptr;
            return head;
        }
        ListNode* slow = head, *fast = head;
        while (fast != tail) {
            slow = slow->next;
            fast = fast->next;
            if (fast != tail) {
                fast = fast->next;
            }
        }
        ListNode* mid = slow;
        return merge(sortList(head, mid), sortList(mid, tail));
    }

    ListNode* merge(ListNode* head1, ListNode* head2) {
        ListNode* dummyHead = new ListNode(0);
        ListNode* temp = dummyHead, *temp1 = head1, *temp2 = head2;
        while (temp1 != nullptr && temp2 != nullptr) {
            if (temp1->val <= temp2->val) {
                temp->next = temp1;
                temp1 = temp1->next;
            } else {
                temp->next = temp2;
                temp2 = temp2->next;
            }
            temp = temp->next;
        }
        if (temp1 != nullptr) {
            temp->next = temp1;
        } else if (temp2 != nullptr) {
            temp->next = temp2;
        }
        return dummyHead->next;
    }
};

三、生词

  • invariably adv. 始终不变地

  • benign adj. 良性的;亲切的

  • wether forecast 天气预报

  • constitute vt. 构成,组成

  • denote vt. 表示

  • cardinal adj. 基本的,主要的

  • diagonal adj. 对角线的,n. 斜线

四、参考文章

  1. POJ - 2044 Weather Forecast
  2. POJ2044 Weather Forecast(DFS)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值