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;
}
anonymous No title
C++
#include<bits/stdc++.h>
using namespace std;

vector<int> uselist[4][4];
bool piece[17];
int field[5][4];
int clist[10][4][2];
stack<int> putp;
stack<int> putx;
stack<int> puty;
vector<int> binex(int a){
    vector<int> ret = {0,0,0,0};
    int cnt = 0;
    while(a>0){
        ret[cnt]=a%2;
        a/=2;
        cnt++;
    }
    return ret;
}
bool check(vector<int> cclist){
    for(int i1 = cclist.size() - 1;i1 >= 0;i1--){
        int now = cclist[i1];
        int tmp[4] = {0,0,0,0};
        for(int i = 0;i<4;i++){
            if(field[clist[now][i][0]][clist[now][i][1]]==-1){
                for(int j = 0;j<4;j++){
                    tmp[j] = 5;
                }
                break;
            }
            vector<int> tmpp = binex(field[clist[now][i][0]][clist[now][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 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;
    putx.pop();
    puty.pop();
    putp.pop();
}
int iswin(int nowhave,int nowdepth){
    if(nowdepth == 0){
        return -1;//0だった
    }
    bool drawflag = false;
    for(int i1 = 0;i1 < 4;i1++){
        for(int i2 = 0;i2 < 4;i2++){
            if(field[i1][i2] == -1){
                deletepiece();
                putpiece(i1,i2,nowhave);
            }else continue;
            if(check(uselist[i1][i2])){
                return 1;
            }
            for(int i3 = 0;i3<16;i3++){
                if(!piece[i3])continue;
                for(int i4 = 0;i4<4;i4++){
                    for(int i5 = 0;i5<4;i5++){
                        if(field[i4][i5] == -1){
                            putpiece(i4,i5,i3);
                        }else continue;
                        if(check(uselist[i4][i5])){
                            goto endloop1;
                        }
                        for(int i6 = 0;i6<16;i6++){
                            if(!piece[i6])continue;
                            putpiece(4,3,16);
                            int willwin = iswin(i6,nowdepth-1);
                            if(willwin == -1){
                                deletepiece();
                                goto endloop1;
                            }else if(willwin == 1){
                                deletepiece();
                            /*}else if(willwin == 0){
                                drawflag = true;
                                deletepiece();*/
                            }
                        }
                        deletepiece();
                    }
                }
                /*if(drawflag){
                    return 0;
                }else{*/
                    return 1;
                //}
                endloop1:;
                deletepiece();
            }
            
        }
    }
    /*if(drawflag){
        return 0;
    }else{*/
        return -1;
    //}
}
int main(){
    for(int i1 = 0;i1 < 4;i1++){
        for(int i2 = 0;i2 < 4;i2++){
            clist[i1][i2][0] = i1;
            clist[i1][i2][1] = i2;
            uselist[i1][i2].push_back(i1);
        }
    }
    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;
            uselist[i2][i1].push_back(i1 + 4);
        }
    }
    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;
        uselist[i2][i2].push_back(8);
        uselist[i2][3 - i2].push_back(9);
    }
    for(int i = 0;i<16;i++){
        piece[i] = true;
    }
    for(int i1 = 0;i1 < 4;i1++){
        for(int i2 = 0;i2 < 4;i2++){
            cin >> field[i1][i2];
            piece[field[i1][i2]] == false;
        }
    }
    int youhave;
    int depth;
    cin >> youhave >> depth;
    putpiece(4,3,16);
    int iwillwin = iswin(youhave,depth);
    if(iwillwin == 1){
        cout << "win" << endl;
        while(!putx.empty()){
            cout << puty.top() << putx.top() << endl;
            cout << putp.top() << endl;
            putx.pop();
            puty.pop();
            putp.pop();
        }
    }else if(iwillwin == 0){
        cout << "draw" << endl;
    }else{
        cout << "lose" << endl;
        while(!putx.empty()){
            cout << puty.top() << putx.top() << endl;
            cout << putp.top() << endl;
            putx.pop();
            puty.pop();
            putp.pop();
        }
    }
    return 0;
}
anonymous No title
C++
#include <iostream>
#include <vector>
#include <set>
using namespace std;
// 今のfieldでputplaceにnowpieceを置いたときに勝ちかどうか
bool iswinvec(std::vector<int> a){
    if(a[0]==-1) return false;
    for(int i = 0;i<4;i++) if(a[i]==4 || a[i]==0) return true;
    return false;
}
std::vector<int> binex(int a){
    std::vector<int> ret = {0,0,0,0};
    int cnt = 0;
    while(a>0){
        ret[cnt]=a%2;
        a/=2;
        cnt++;
    }
    return ret;
}
bool iswin(std::vector<std::vector<int>> field,std::pair<int,int> putplace,int nowpiece){
    field[putplace.first][putplace.second]=nowpiece;
    for(int i = 0;i<4;i++){
        std::vector<int> tmp = {0,0,0,0};
        for(int j = 0;j<4;j++){
            if(field[i][j]<0){
                tmp = {-1,0,0,0};
                break;
            }
            std::vector<int> binextmp = binex(field[i][j]);
            for(int k = 0;k<4;k++){
                tmp[k]+=binextmp[k];
            }
        }
        if(iswinvec(tmp)) return true;
    }
    for(int i = 0;i<4;i++){
        std::vector<int> tmp = {0,0,0,0};
        for(int j = 0;j<4;j++){
            if(field[j][i]<0){
                tmp = {-1,0,0,0};
                break;
            }
            std::vector<int> binextmp = binex(field[j][i]);
            for(int k = 0;k<4;k++){
                tmp[k]+=binextmp[k];
            }
        }
        if(iswinvec(tmp)) return true;
    }
    for(int i = 0;i<1;i++){
        std::vector<int> tmp = {0,0,0,0};
        for(int j = 0;j<4;j++){
            if(field[j][j]<0){
                tmp = {-1,0,0,0};
                break;
            }
            std::vector<int> binextmp = binex(field[j][j]);
            for(int k = 0;k<4;k++){
                tmp[k]+=binextmp[k];
            }
        }
        if(iswinvec(tmp)) return true;
    }
    for(int i = 0;i<1;i++){
        std::vector<int> tmp = {0,0,0,0};
        for(int j = 0;j<4;j++){
            if(field[3-j][j]<0){
                tmp = {-1,0,0,0};
                break;
            }
            std::vector<int> binextmp = binex(field[3-j][j]);
            for(int k = 0;k<4;k++){
                tmp[k]+=binextmp[k];
            }
        }
        if(iswinvec(tmp))  return true;
    }
    return false;
}
std::vector<std::vector<int>> put(std::vector<std::vector<int>> field,std::pair<int,int> putplace,int nowpiece){
    field[putplace.first][putplace.second]=nowpiece;
    return field;
}
std::set<int> erase(std::set<int> remainpiece,int piece){
    remainpiece.erase(piece);
    return remainpiece;
}
std::vector<int> solve(std::vector<std::vector<int>> field,std::set<int> remainpiece,int nowpiece,int depth){
    //今おけるところを調べている
    std::vector<std::pair<int,int>> canput;
    for(int i=0;i<4;i++) for(int j=0;j<4;j++) if(field[i][j]<0) canput.push_back(std::make_pair(i,j));
    //今の一手で勝てるかどうかを調べてる
    for(int i=0;i<canput.size();i++){
        if(iswin(field,canput[i],nowpiece)){
            std::vector<int> ret = {1,canput[i].first,canput[i].second};
            //cout << nowpiece << endl;
            return ret;
        }
    }
    if(depth==1){
        std::vector<int> ret = {0,-1,-1};
        return ret;
    }
    std::vector<int> ret = {-1,-1,-1};
    int sign = -1;
    for(int i=0;i<canput.size();i++){
        for(auto itr = remainpiece.begin();itr!=remainpiece.end();itr++){
            std::vector<int> tmptmpret = solve(put(field,canput[i],nowpiece),erase(remainpiece,*itr),*itr,depth-1);
            tmptmpret[0]*=(-1);
            if(tmptmpret[0]==1){
                ret[0]=1;
                ret[1]=canput[i].first;
                ret[2]=canput[i].second;
                ret.push_back(*itr);
                //cout << ret[1] << " " << ret[2] << endl;
                return ret;
            };
            sign = max(tmptmpret[0],sign);
        }
        if(sign>ret[0]) ret[0] = sign;
        //if(ret[0]==1) return ret;
    }
    return ret;
}
int main(){
    std::vector<std::vector<int>> field;
   for(int i = 0;i<4;i++) field.push_back({});
    for(int i = 0;i<4;i++) for(int j = 0;j<4;j++) field[i].push_back(-1);
    for(int i = 0;i<4;i++) for(int j = 0;j<4;j++) std::cin >> field[i][j];
    int nowpiece,depth;
    std::cin >> nowpiece;
    std::cin >> depth;
    std::set<int> remainpiece;
    for(int i = 0;i<16;i++) remainpiece.insert(i);
    for(int i = 0;i<4;i++) for(int j = 0;j<4;j++){
        if(field[i][j]!=-1) remainpiece.erase(field[i][j]);
    }
    remainpiece.erase(nowpiece);
    std::vector<int> result = solve(field,remainpiece,nowpiece,depth);
    std::cout << result[0] << " " << result[1] << " " << result[2] << std::endl;
    if(result.size()==4) cout << result[3] << endl;
}
anonymous No title
C++
#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i=0;i<int(N);++i)

typedef long long ll;
const int dx[2] = {1, 0};
const int dy[2] = {0, 1};

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

const int INF = 1e9;

int T[123][123] = {};
ll Cost[123][123] =  {};
int H,W,C;

bool IsIn(int x,int y){
    return 0<=x&&x<H&&0<=y&&y<W;
}
int main() {
    cin>>H>>W>>C;
    rep(i,H)rep(j,W){
        cin>>T[i][j];
        Cost[i][j] = INF;
    }
    Cost[0][0] = 0;
    queue<pair<int,int>> Q;
    Q.push({0,0});
    while(!Q.empty()){
        auto p =  Q.front();
        int x = p.first;
        int y = p.second;
        Q.pop();
        rep(i,2){
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(!IsIn(nx,ny))continue;
            if(T[nx][ny]==1){
                //chmin(Cost[nx][ny], Cost[x][y] + C);
                Cost[nx][ny] = min(Cost[nx][ny], Cost[x][y] + C);
            }
            else{
                Cost[nx][ny] = min(Cost[nx][ny], Cost[x][y]);
                //chmin(Cost[nx][ny], Cost[x][y]);
            }
            Q.push({nx,ny});
        }
    }
    cout<<Cost[H-1][W-1]<<endl;
}
Don't you submit code?
Submit
年末年始は機械学習・深層学習を勉強しませんか?
広告
未経験から最短でエンジニアへの転職を目指すなら