anonymous No title
C++
// キューの大きさ
#define QUEUE_LENGTH 16

// 通常のキュー
QueueHandle_t xQueue;

TaskHandle_t taskHandle;

void task1(void *pvParameters) {
  while (1) {
    uint32_t data;
    if (xQueueReceive(xQueue, &data, 0) == pdTRUE) {
      // キューを受信するまで待つ
      Serial.printf("data = %d\n", data);
    }
    delay(1);
  }
}

void setup() {
  Serial.begin(115200);
  delay(100);

  // キュー作成
  xQueue = xQueueCreate(QUEUE_LENGTH, sizeof(uint8_t));

  // Core1の優先度1で通知受信用タスク起動
  xTaskCreateUniversal(
    task1,
    "task1",
    8192,
    NULL,
    1,
    &taskHandle,
    APP_CPU_NUM
  );
}

int i = 0;
void loop() {
  i++;
  if (i % 10 == 0) {
    uint32_t data = i;
    uint8_t ret;

    // キューを送信
    ret = xQueueSend(xQueue, &data, 0);
  }

  delay(100);
}
anonymous No title
C++
/*
 copy folder and its contents.
 dest: destination directory
 source: source directory
 hwnd: window handle of caller. Security attribute is the same of hwnd owner.
 return: 0 if succeeded. otherwise non zero value.
*/
int copy_folder(const String& path_dest, const String& path_source, HWND hwnd) {
    SHFILEOPSTRUCT tSHFile = { 0 };
    tSHFile.hwnd = (hwnd == NULL) ? ::GetDesktopWindow() : hwnd;
    tSHFile.wFunc = FO_COPY;
    tSHFile.fFlags = FOF_SILENT | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI;
    tSHFile.fAnyOperationsAborted = TRUE;
    tSHFile.hNameMappings = NULL;
    tSHFile.lpszProgressTitle = _T("");

    // add \0\0 to the tail of folder name.
    // std::vector is used because std::string's operand+ can not add \0 (\0 is ignored).
    std::vector<TCHAR> pfrom(path_source.size() + 2);
    _tcscpy_s(pfrom.data(), path_source.size() + 2, path_source.c_str());
    pfrom[pfrom.size() - 1] = _T('\0');
    tSHFile.pFrom = pfrom.data();

    std::vector<TCHAR> pto(path_dest.size() + 2);
    _tcscpy_s(pto.data(), path_dest.size() + 2, path_dest.c_str());
    pto[pto.size() - 1] = _T('\0');
    tSHFile.pTo = pto.data();

    return SHFileOperation(&tSHFile);
}
anonymous No title
C++
#include<bits/stdc++.h>
#include<time.h>
using namespace std;
bool hlflag;
static int field[8][8][8];
static bool piece[512];
static int xlength,ylength,usefnum,usepnum,useppow,clistnum;
static int zlength;
static int clist[20000][4][3];
static vector<int> useclist[8][8][8];
static stack<int> puthis[4];
static int lputhis[5];
static vector<int> reachnum;
static void putpiece(int z,int y,int x,int nowhave){
    field[z][y][x] = nowhave;
    puthis[0].push(z);
    puthis[1].push(y);
    puthis[2].push(x);
    puthis[3].push(nowhave);
    piece[nowhave] = false;
}
static void deletepiece(){
    lputhis[4] = lputhis[3];
    for(int i = 0;i<4;i++){
        lputhis[i] = puthis[i].top();
        puthis[i].pop();
    }
    field[lputhis[0]][lputhis[1]][lputhis[2]] = -1;
    piece[lputhis[3]] = true;
}
static void shitazyunbi(){
    for(int i = 0;i<512;i++){
        piece[i] = true;
    }
    for(int i = 0;i<5;i++){
        lputhis[i] = -1;
    }
    usefnum = xlength*ylength*zlength;
    usepnum = 1;
    useppow = 0;
    while(usepnum<usefnum){
        usepnum += usepnum;
        useppow++;
    }
    for(int i = 0;i<useppow*2;i++){
        reachnum.push_back(0);
    }
}
static void makeclist(){
    int nowlistnum = 0;
    for(int i1 = 0;i1<zlength;i1++){
        for(int i2 = 0;i2<ylength;i2++){
            for(int i3 = 0;i3<xlength;i3++){
                if(i3<xlength-3){
                    for(int i4 = 0;i4<4;i4++){
                        clist[nowlistnum][i4][0] = i1;
                        clist[nowlistnum][i4][1] = i2;
                        clist[nowlistnum][i4][2] = i3+i4;
                        useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                    }
                    nowlistnum++;
                    if(i2<ylength-3){
                        for(int i4 = 0;i4<4;i4++){
                            clist[nowlistnum][i4][0] = i1;
                            clist[nowlistnum][i4][1] = i2+i4;
                            clist[nowlistnum][i4][2] = i3+i4;
                            useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                        }
                        nowlistnum++;
                        for(int i4 = 0;i4<4;i4++){
                            clist[nowlistnum][i4][0] = i1;
                            clist[nowlistnum][i4][1] = i2+3-i4;
                            clist[nowlistnum][i4][2] = i3+i4;
                            useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                        }
                        nowlistnum++;
                        if(i1<zlength-3){
                            for(int i4 = 0;i4<4;i4++){
                                clist[nowlistnum][i4][0] = i1+i4;
                                clist[nowlistnum][i4][1] = i2+i4;
                                clist[nowlistnum][i4][2] = i3+i4;
                                useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                            }
                            nowlistnum++;
                            for(int i4 = 0;i4<4;i4++){
                                clist[nowlistnum][i4][0] = i1+3-i4;
                                clist[nowlistnum][i4][1] = i2+i4;
                                clist[nowlistnum][i4][2] = i3+i4;
                                useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                            }
                            nowlistnum++;
                            for(int i4 = 0;i4<4;i4++){
                                clist[nowlistnum][i4][0] = i1+i4;
                                clist[nowlistnum][i4][1] = i2+3-i4;
                                clist[nowlistnum][i4][2] = i3+i4;
                                useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                            }
                            nowlistnum++;
                            for(int i4 = 0;i4<4;i4++){
                                clist[nowlistnum][i4][0] = i1+i4;
                                clist[nowlistnum][i4][1] = i2+i4;
                                clist[nowlistnum][i4][2] = i3+3-i4;
                                useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                            }
                            nowlistnum++;
                        }
                    }
                }
                if(i2<ylength-3){
                    for(int i4 = 0;i4<4;i4++){
                        clist[nowlistnum][i4][0] = i1;
                        clist[nowlistnum][i4][1] = i2+i4;
                        clist[nowlistnum][i4][2] = i3;
                        useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                    }
                    nowlistnum++;
                    if(i1<zlength-3){
                        for(int i4 = 0;i4<4;i4++){
                            clist[nowlistnum][i4][0] = i1+i4;
                            clist[nowlistnum][i4][1] = i2+i4;
                            clist[nowlistnum][i4][2] = i3;
                            useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                        }
                        nowlistnum++;   
                        for(int i4 = 0;i4<4;i4++){
                            clist[nowlistnum][i4][0] = i1+3-i4;
                            clist[nowlistnum][i4][1] = i2+i4;
                            clist[nowlistnum][i4][2] = i3;
                            useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                        }
                        nowlistnum++;          
                    }
                }
                if(i1<zlength-3){
                    for(int i4 = 0;i4<4;i4++){
                        clist[nowlistnum][i4][0] = i1+i4;
                        clist[nowlistnum][i4][1] = i2;
                        clist[nowlistnum][i4][2] = i3;
                        useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                    }
                    nowlistnum++;
                    if(i3<xlength-3){
                        for(int i4 = 0;i4<4;i4++){
                            clist[nowlistnum][i4][0] = i1+i4;
                            clist[nowlistnum][i4][1] = i2;
                            clist[nowlistnum][i4][2] = i3+i4;
                            useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                        }
                        nowlistnum++;
                        for(int i4 = 0;i4<4;i4++){
                            clist[nowlistnum][i4][0] = i1+3-i4;
                            clist[nowlistnum][i4][1] = i2;
                            clist[nowlistnum][i4][2] = i3+i4;
                            useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                        }
                        nowlistnum++;
                    }
                }
            }
        }
    }
    if(hlflag){
        for(int i1 = 0;i1<3;i1++){
            for(int i2 = 0;i2<3;i2++){
                for(int i4 = 0;i4<4;i4++){
                    clist[nowlistnum][i4][0] = 0;
                    clist[nowlistnum][i4][1] = i1+(i4%2);
                    clist[nowlistnum][i4][2] = i2+(i4/2);
                    useclist[clist[nowlistnum][i4][0]][clist[nowlistnum][i4][1]][clist[nowlistnum][i4][2]].push_back(nowlistnum);
                }
                nowlistnum++;
            }
        }
    }
    clistnum = nowlistnum;
}
static vector<int> binkai(int a){
    vector<int> ret; 
    for(int i = 0;i<useppow;i++){
        ret.push_back(-1);
    }
    int cnt = 0;
    while(a>0){
        ret[cnt]=a%2+a%2-1;
        a/=2;
        cnt++;
    }
    return ret;
}
static int firstcheck(int nowhave){
    int wlist = -1;
    for(int i1 = clistnum-1;i1>-1;i1--){
        int nowlist = i1;
        vector<int> binnhave = binkai(nowhave);
        vector<int> sum(useppow);
        for(int i = sum.size()-1;i>-1;i--){
            sum[i] = 0;
        }
        bool spaceflag = false;
        for(int i2 = 0;i2<4;i2++){
            int cpiece = field[clist[nowlist][i2][0]][clist[nowlist][i2][1]][clist[nowlist][i2][2]];
            if(cpiece != -1){
                vector<int> tmp = binkai(cpiece);
                for(int i3 = 0;i3<useppow;i3++){
                    sum[i3] += tmp[i3];
                }
            }else{
                if(spaceflag){
                    break;
                }
                spaceflag = true;
            }
        }
        if(!spaceflag){
            continue;
        }
        for(int i2 = 0;i2<useppow;i2++){
            if(sum[i2] == 3){
                reachnum[i2+i2+1]++;
                if(binnhave[i2] == 1){
                    wlist = nowlist;
                }
            }
            if(sum[i2] == -3){
                reachnum[i2+i2]++;
                if(binnhave[i2] == -1){
                    wlist = nowlist;
                }
            }
        }
    }
    return wlist;
}
static vector<int> recheck(int z,int y,int x){
    vector<int> kaeriti(useppow*2);
    for(int i1 = useclist[z][y][x].size()-1;i1>-1;i1--){
        int nowlist = useclist[z][y][x][i1];
        vector<int> 
anonymous タッチとGPIOを同時に使う方法
C++
volatile uint32_t touchedTime = 0;

void IRAM_ATTR gotTouch() {
  touchedTime = millis();
}

void setup() {
  Serial.begin(115200);
  delay(50);

  touchAttachInterrupt(33, gotTouch, 10);
  pinMode(33, OUTPUT);
}

void loop() {
  if (millis() < touchedTime + 50) {
    digitalWrite(33, HIGH);
  } else {
    digitalWrite(33, LOW);
  }
  delay(1);
}
anonymous No title
C++
#include <M5StickC.h>

void setup() {
  M5.begin();
  M5.Lcd.fillScreen(BLACK);

  Wire1.beginTransmission(0x34);
  Wire1.write(0x33);
  Wire1.write(0x00);
  Wire1.endTransmission();
}

void loop() {
  M5.Lcd.setCursor(0, 4, 1);

  M5.Lcd.printf("AXP192 Test\n");
  M5.Lcd.printf("\n");

  M5.Lcd.printf("Battery\n");
  M5.Lcd.printf(" State:%6d\n"  , M5.Axp.GetBatState());      // バッテリーが接続されているか(常に1のはず)
  M5.Lcd.printf(" Warn :%6d\n"  , M5.Axp.GetWarningLevel());  // バッテリー残量警告 0:残あり, 1:残なし
  M5.Lcd.printf(" Temp :%6.1f\n", M5.Axp.GetTempInAXP192());  // AXP192の内部温度
  M5.Lcd.printf(" V(V) :%6.3f\n", M5.Axp.GetBatVoltage());    // バッテリー電圧(3.0V-4.2V程度)
  M5.Lcd.printf(" I(mA):%6.1f\n", M5.Axp.GetBatCurrent());    // バッテリー電流(プラスが充電、マイナスが放電)
  M5.Lcd.printf(" W(mW):%6.1f\n", M5.Axp.GetBatPower());      // バッテリー電力(W=V*abs(I))

  M5.Lcd.printf("ASP\n");
  M5.Lcd.printf(" V(V) :%6.3f\n", M5.Axp.GetAPSVoltage());    // ESP32に供給されている電圧

  M5.Lcd.printf("VBus(USB)\n");
  M5.Lcd.printf(" V(V) :%6.3f\n", M5.Axp.GetVBusVoltage());   // USB電源からの電圧
  M5.Lcd.printf(" I(mA):%6.1f\n", M5.Axp.GetVBusCurrent());   // USB電源からの電流

  M5.Lcd.printf("VIN(5V-In)\n");
  M5.Lcd.printf(" V(V) :%6.3f\n", M5.Axp.GetVinVoltage());    // 5V IN端子からの電圧
  M5.Lcd.printf(" I(mA):%6.1f\n", M5.Axp.GetVinCurrent());    // 5V IN端子からの電流

//  delay(1000);
}
anonymous No title
C++
int main() {
    init();
    ll N;
    cin >> N;
    string S;
    cin >> S;
    ll Q;
    cin >> Q;
    vc<set<ll>> ss(26);
    rep(i,N){
        ss[S[i] - 'a'].emplace(i);
    }

    while(Q--){
        ll q;
        cin >> q;
        if(q==1){
            ll x;
            char c;
            cin >> x >> c;
            x--;
            if (S[x] == c) continue;

            ss[S[x] - 'a'].erase(x);
            ss[c - 'a'].emplace(x);
        }else{
            ll l, r;
            cin >> l >> r;
            l--, r--;
            ll ans = 0;
            rep(i,26){
                auto t = ss[i].lower_bound(l);
                if(t==ss[i].end()){
                    continue;
                }
                else if(*t<=r)
                    ans++;
            }
            cout << ans << endl;
        }
    }
}
anonymous No title
C++
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <boost/format.hpp>
#include <random>
#include <algorithm>
using namespace std;

const int N = 4;
const int NN = N * N;
const int MaxPlace = 9;
const int Trial = 10000000;

using conf_t = vector<int>;

inline size_t pos(int i, int j){
  return i + N * j;
}

// 配置の出力用関数
inline string conf2str(const conf_t &xs){
  string ret = "";
  for(int i=0; i<N; ++i){
    ret += (boost::format("%2d %2d %2d %2d\n")
      % xs[pos(0,i)] % xs[pos(1,i)] % xs[pos(2,i)] % xs[pos(3,i)]).str();
  }
  return ret;
}

// 空の場所のリスト生成
vector<size_t> empty_place(const conf_t &xs){
  vector<size_t> ret;
  for(size_t i=0; i<xs.size(); ++i){
    if(!xs[i]) ret.push_back(i);
  }
  return ret;
}

// 空の場所をランダムに埋める
conf_t place_rnd(const conf_t &xs, vector<size_t> &empty, mt19937 &engine){
  conf_t ret(xs);
  // 空の場所のリストをシャッフル
  shuffle(empty.begin(), empty.end(), engine);
  for(size_t i=0; i<empty.size() - (NN - MaxPlace); ++i){
    ret[empty[i]] = 1;
  }
  return ret;
}

// ビンゴの数をカウント
int hantei(const conf_t &xs){
  int ret = 0;
  vector<int> sums(2*N + 2, 0);
  for(int i=0; i<N; ++i){
    for(int j=0; j<N; ++j){
      sums[j] += xs[pos(i, j)];
      sums[j + N] += xs[pos(j, i)];
    }
    sums[2*N] += xs[pos(i,i)];
    sums[2*N + 1] += xs[pos(N-1-i, i)];
  }
  for(auto x : sums){
    if(x == N) ++ret;
  }
  return ret;
}

conf_t configuration(NN), rev(NN);

int main(){
  // 乱数初期化
  std::random_device seed_gen;
  std::mt19937 engine(1983);

  // 入力を受け取る
  int nonzero = 0;
  for(int j=0; j<N; ++j){
    for(int i=0; i<N; ++i){
      size_t n;
      cin >> n;
      configuration[pos(i,j)] = n;
      if(n != 0) ++nonzero;
    }
  }
  cout << conf2str(configuration);
  cout << nonzero << endl;

  // 空の場所のリストを生成
  auto empty = empty_place(configuration);
  // 結果のヒストグラム
  vector<int> histogram(4,0);
  for(int i=0; i<Trial; ++i){
    auto placed = place_rnd(configuration, empty, engine);
    histogram[hantei(placed)] += 1;
  }
  // 結果を出力
  for(size_t i=0; i < histogram.size(); ++i){
    cout << (boost::format("%d\t%8d\t%0.4e\n") % i % histogram[i] % (histogram[i] / double(Trial))).str();
  }
  return 0;
}
anonymous No title
C++
clist[30][4][2];
void makeclist(){
    for(int i1 = 0;i1 < 4;i1++){
        for(int i2 = 0;i2 < 4;i2++){
            clist[i1][i2][0] = i1;
            clist[i1][i2][1] = i2;
        }
    }
    for(int i1 = 0;i1 < 4;i1++){
         for(int i2 = 0;i2 < 4;i2++){
            clist[i1 + 4][i2][0] = i2;
            clist[i1 + 4][i2][1] = i1;
        }
    }
    for(int i2 = 0;i2 < 4;i2++){
        clist[8][i2][0] = i2;
        clist[8][i2][1] = i2;
        clist[9][i2][0] = i2;
        clist[9][i2][1] = 3 - i2;
    }
    for(int i1 = 0;i1<3;i1++){
        for(int i2 = 0;i2<3;i2++){
            for(int i3 = 0;i3<2;i3++){
                for(int i4 = 0;i4<2;i4++){
                    clist[10+3*i1+i2][i3+i3+i4][0] = i1+i3;
                    clist[10+3*i1+i2][i3+i3+i4][1] = i2+i4;
                }
            }
        }
    }
    for(int i1 = 1;i1<3;i1++){
        for(int i2 = 1;i2<3;i2++){
            for(int i3 = -1;i3<2;i3+=2){
                for(int i4 = -1;i4<2;i4+=2){
                    clist[16+i1+i1+i2][1+i3+(i4+1)/2][0] = i1+i3;
                    clist[16+i1+i1+i2][1+i3+(i4+1)/2][1] = i2+i4;
                    clist[20+i1+i1+i2][1+i3+(i4+1)/2][0] = i1+(i3+i4)/2;
                    clist[20+i1+i1+i2][1+i3+(i4+1)/2][1] = i2+(i3-i4)/2;
                }
            }           
        }
    }
    clist[27][0][0] = 0;
    clist[27][1][0] = 1;
    clist[27][2][0] = 3;
    clist[27][3][0] = 2;
    clist[28][0][0] = 0;
    clist[28][1][0] = 2;
    clist[28][2][0] = 3;
    clist[28][3][0] = 1;
    clist[29][0][0] = 0;
    clist[29][1][0] = 0;
    clist[29][2][0] = 3;
    clist[29][3][0] = 3;
    clist[27][0][1] = 1;
    clist[27][1][1] = 3;
    clist[27][2][1] = 2;
    clist[27][3][1] = 0;
    clist[28][0][1] = 2;
    clist[28][1][1] = 3;
    clist[28][2][1] = 1;
    clist[28][3][1] = 0;
    clist[29][0][1] = 0;
    clist[29][1][1] = 3;
    clist[29][2][1] = 3;
    clist[29][3][1] = 0;
}
anonymous No title
C++
int clist[30][4][2];
void makeclist(){
    for(int i1 = 0;i1 < 4;i1++){
        for(int i2 = 0;i2 < 4;i2++){
            clist[i1][i2][0] = i1;
            clist[i1][i2][1] = i2;
        }
    }
    for(int i1 = 0;i1 < 4;i1++){
         for(int i2 = 0;i2 < 4;i2++){
            clist[i1 + 4][i2][0] = i2;
            clist[i1 + 4][i2][1] = i1;
        }
    }
    for(int i2 = 0;i2 < 4;i2++){
        clist[8][i2][0] = i2;
        clist[8][i2][1] = i2;
        clist[9][i2][0] = i2;
        clist[9][i2][1] = 3 - i2;
    }
    for(int i1 = 0;i1<3;i1++){
        for(int i2 = 0;i2<3;i2++){
            for(int i3 = 0;i3<2;i3++){
                for(int i4 = 0;i4<2;i4++){
                    clist[10+3*i1+i2][i3+i3+i4][0] = i1+i3;
                    clist[10+3*i1+i2][i3+i3+i4][1] = i2+i4;
                }
            }
        }
    }
    for(int i1 = 1;i1<3;i1++){
        for(int i2 = 1;i2<3;i2++){
            for(int i3 = -1;i3<2;i3+=2){
                for(int i4 = -1;i4<2;i4+=2){
                    clist[16+i1+i1+i2][i3+(i4+1)/2][0] = i1+i3;
                    clist[16+i1+i1+i2][i3+(i4+1)/2][1] = i2+i4;
                    clist[20+i1+i1+i2][i3+(i4+1)/2][0] = i1+(i3+i4)/2;
                    clist[20+i1+i1+i2][i3+(i4+1)/2][1] = i2+(i3-i4)/2;
                }
            }           
        }
    }
    clist[27][0][0] = 0;
    clist[27][1][0] = 1;
    clist[27][2][0] = 3;
    clist[27][3][0] = 2;
    clist[28][0][0] = 0;
    clist[28][1][0] = 2;
    clist[28][2][0] = 3;
    clist[28][3][0] = 1;
    clist[29][0][0] = 0;
    clist[29][1][0] = 0;
    clist[29][2][0] = 3;
    clist[29][3][0] = 3;
    clist[27][0][1] = 1;
    clist[27][1][1] = 3;
    clist[27][2][1] = 2;
    clist[27][3][1] = 0;
    clist[28][0][1] = 2;
    clist[28][1][1] = 3;
    clist[28][2][1] = 1;
    clist[28][3][1] = 0;
    clist[29][0][1] = 0;
    clist[29][1][1] = 3;
    clist[29][2][1] = 3;
    clist[29][3][1] = 0;
}
anonymous No title
C++
#include<bits/stdc++.h>
#include<time.h>
using namespace std;
int lastx,lasty;
int lastpiece = -1;
int llastpiece = -1;
bool notloseflag = false;
stack<int> putp;
stack<int> putx;
stack<int> puty;
bool piece[16];
bool cantgive[8];
int field[4][4];
int clist[10][4][2];
void putpiece(int y,int x,int piecenum){
    putx.push(x);
    puty.push(y);
    putp.push(piecenum);
    field[y][x] = piecenum;
    piece[piecenum] = false;
}
void deletepiece(){
    piece[putp.top()] = true;
    field[puty.top()][putx.top()] = -1;
    lastx = putx.top();
    lasty = puty.top();
    llastpiece = lastpiece;
    lastpiece = putp.top();
    putx.pop();
    puty.pop();
    putp.pop();
}
vector<int> binx(int a){
    vector<int> ret = {0,0,0,0};
    int cnt = 0;
    while(cnt<4){
        ret[cnt]=a%2;
        a/=2;
        cnt++;
    }
    return ret;
}
vector<int> binkai(int a){
    vector<int> ret = {0,0,0,0};
    int cnt = 0;
    while(cnt<4){
        ret[cnt]=a%2+a%2-1;
        a/=2;
        cnt++;
    }
    return ret;
}
void check(){
    for(int i1 = 0;i1<8;i1++){
        cantgive[i1] = false;
    }
    for(int i1 = 0;i1<10;i1++){
        int sum[4] = {0,0,0,0};
        bool spaceflag = false;
        for(int i2 = 0;i2<4;i2++){
            int cpiece = field[clist[i1][i2][0]][clist[i1][i2][1]];
            if(cpiece != -1){
                vector<int> tmp = binkai(cpiece);
                for(int i3 = 0;i3<4;i3++){
                    sum[i3] += tmp[i3];
                }
            }else{
                if(spaceflag){
                    break;
                }
                spaceflag = true;
            }
        }
        if(!spaceflag){
            continue;
        }
        for(int i2 = 0;i2<4;i2++){
            if(sum[i2] == 3){
                cantgive[i2+i2+1] = true;
            }
            if(sum[i2] == -3){
                cantgive[i2+i2] = true;
            }
        }
    }
}
bool putcheck(int nowhave){
    vector<int> tmp = binx(nowhave);
    for(int i1 = 0;i1<4;i1++){
        if(cantgive[i1+i1+tmp[i1]]){
            return true;
        }
    }
    return false;
}
bool putedcheck(){
    for(int i1 = 0;i1<10;i1++){
        int tmp[4] = {0,0,0,0};
        for(int i = 0;i<4;i++){
            if(field[clist[i1][i][0]][clist[i1][i][1]]==-1){
                for(int j = 0;j<4;j++){
                    tmp[j] = 5;
                }
                break;
            }
            vector<int> tmpp = binx(field[clist[i1][i][0]][clist[i1][i][1]]);
            for(int j = 0;j<4;j++) tmp[j]+=tmpp[j];
        }
        for(int i = 0;i<4;i++) if(tmp[i]==0 || tmp[i]==4) return true;
    }
    return false;
}
void ittedume(){
    for(int i1 = 0;i1<16;i1++){
        if(!piece[i1])continue;
        for(int i2 = 0;i2<4;i2++){
            for(int i3 = 0;i3<4;i3++){
                if(field[i2][i3] != -1)continue;
                putpiece(i2,i3,i1);
                if(putedcheck()){
                    deletepiece();
                    llastpiece = -1;
                    return;
                }
                deletepiece();
            }
        }
    }
}
void makeclist(){
    for(int i1 = 0;i1 < 4;i1++){
        for(int i2 = 0;i2 < 4;i2++){
            clist[i1][i2][0] = i1;
            clist[i1][i2][1] = i2;
        }
    }
    for(int i1 = 0;i1 < 4;i1++){
         for(int i2 = 0;i2 < 4;i2++){
            clist[i1 + 4][i2][0] = i2;
            clist[i1 + 4][i2][1] = i1;
        }
    }
    for(int i2 = 0;i2 < 4;i2++){
        clist[8][i2][0] = i2;
        clist[8][i2][1] = i2;
        clist[9][i2][0] = i2;
        clist[9][i2][1] = 3 - i2;
    }
}
bool iswin(int nowhave,int nowdepth){
    check();
    if(putcheck(nowhave)){
        return true;
    }
    if(nowdepth == 0){
        return false;
    }
    for(int i1 = 0;i1<4;i1++){
        for(int i2 = 0;i2<4;i2++){
            if(field[i1][i2] != -1){
                continue;
            }
            putpiece(i1,i2,nowhave);
            for(int i3 = 0;i3<16;i3++){
                if(!piece[i3]){
                    continue;
                }
                if(!iswin(i3,nowdepth - 1)){
                    deletepiece();
                    return true;
                }
            }
            deletepiece();
        }
    }
    return false;
}
int main(){
    makeclist();
    for(int i = 0;i<16;i++){
        piece[i] = true;
    }
    int nokori = 0;
    for(int i1 = 0;i1 < 4;i1++){
        for(int i2 = 0;i2 < 4;i2++){
            cin >> field[i1][i2];
            piece[field[i1][i2]] = false;
            if(field[i1][i2] == -1){
                nokori++;
            }
        }
    }
    int youhave;
    clock_t startt,endt;
    cin >> youhave;
    for(int i = 0;i<nokori;i++){
        lastpiece = -1;
        llastpiece = -1;
        bool iwillwin;
        if(i%2 == 0){
            startt = clock();
            iwillwin = iswin(youhave,i);
            endt = clock();
        }else{
            startt = clock();
            iwillwin = iswin(youhave,i);
            endt = clock();
            if(i == 1){
                putpiece(lasty,lastx,lastpiece);
                check();
                for(int i3 = 0;i3<16;i3++){
                    if(!piece[i3]){
                        continue;
                    }
                    if(!putcheck(i3)){
                        lastpiece = i3;
                        break;
                                            }
                }
                deletepiece();
            }
        }
        cout << "depth = " << i + 1 << " ";
        printf("%f", (double)(endt - startt) / CLOCKS_PER_SEC);
        cout << "sec" << endl;
        if(iwillwin && i%2 == 0){
            if(i == 0){
                ittedume();
            }
            cout << "win " << endl;
            cout << "put " << lastpiece << " (" << lasty << "," << lastx << ") ";
            cout << "give " << llastpiece << endl;
            return 0;
        }else if(iwillwin || i%2 == 0){
            if(i%2 == 0){
                cout << "notwin" << endl;
            }else{
                cout << "notlose" << endl;
                cout << "put " << lastpiece << " (" << lasty << "," << lastx << ") ";
                cout << "give " << llastpiece << endl;
            }
        }else{
            cout << "lose" << endl;
            return 0;
        }
        cout << endl;
    }
    return 0;
}
Don't you submit code?
Submit