C++ code

Want to move your code and projects to LiveCode but don't know where to start?

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller

Post Reply
MaxV
Posts: 1507
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

C++ code

Post by MaxV » Tue Jan 10, 2017 5:04 pm

Hi,
can you convert to livecode these few lines of C++?
I have no idea what they really do, but they works for a task...

Code: Select all

#include <iostream>
#include <vector>
#include <array>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))
using namespace std;
void solve() {
    int n; cin >> n;
    array<string,2> s; cin >> s[0] >> s[1];
    s[0] = "X" + s[0] + "X";
    s[1] = "X" + s[1] + "X";
    int ans = 0;
    repeat (y,2) {
        for (int l = 1; l <= n; ++l) {
            if (s[y][l] == 'X') continue;
            int r = l+1;
            while (s[y][r] != 'X') ++ r;
            if (r-l == 1) continue;
            repeat_from (x,l,r) {
                if (s[(y+1)%2][x-1] == 'X' and s[(y+1)%2][x] == '.' and s[(y+1)%2][x+1] == 'X') {
                    s[(y+1)%2][x] = 'X';
                    break;
                }
            }
            repeat_from (x,l,r) s[y][x] = 'X';
            ans += 1;
            l = r;
        }
    }
    repeat_from (x,1,n+1) {
        if (s[0][x] == '.' or s[1][x] == '.') {
            ans += 1;
        }
    }
    cout << ans << endl;
}
int main() {
    int testcases; cin >> testcases;
    repeat (testcase, testcases) {
        cout << "Case #" << testcase+1 << ": ";
        solve();
    }
    return 0;
}
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 2598
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

Re: C++ code

Post by richmond62 » Tue Jan 10, 2017 5:07 pm

No I cannot, because I've spent the last 14 years learning LiveCode just so I don't have to learn C++

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6044
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: C++ code

Post by FourthWorld » Tue Jan 10, 2017 6:41 pm

What is the task?
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

MaxV
Posts: 1507
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: C++ code

Post by MaxV » Wed Jan 11, 2017 10:15 am

FourthWorld wrote:What is the task?
I'll try to explain easily, it solves a "minimum required ..." task.
It takes an input like:

Code: Select all

.....X.
.X.....
and found how chess tower (or rook) are required to cover all dots, X are walls.
Example solution is 2, because just 2 chess towers can see all dots:

Code: Select all

T....X.
.X....T
This C++ code can solve 307 cases with solution greater than 410 in few seconds, but I can't really understand how it works.
This is how a rook see:
Image
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w

MaxV
Posts: 1507
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: C++ code

Post by MaxV » Wed Jan 11, 2017 11:07 am

Ok I converted the C++ solve() function in Livecode, I missed the ">>" meaning in C++
Now I have to study why it works... :lol: (edited the 12/Jan/2016)

########CODE to copy and paste#######
function solve testo
put the number of chars of line 1 of testo into n
put line 1 of testo into s[0] #for example ".....X."
put line 2 of testo into s[1] #for example ".X....."
put "X" & s[0] & "X" into s[0] #it becames "X.....X.X"
put "X" & s[1] & "X" into s[1] #it becames "X.X.....X"
put 0 into ans
repeat with y=0 to 1
repeat with l=1 to n
if char l of s[y] is "x" then next repeat
put l + 1 into r
repeat while char r of s[y] is not "X"
add 1 to r
end repeat
if (r - l ) is 1 then next repeat
repeat with x=l to (r - 1)
if ( char (x-1) of s[(y + 1) mod 2] is "X" ) AND (char x of s[(y+1) mod 2] is ".") AND ( char (x + 1) of s[(y +1) mod 2] is "X") then
put "X" into char x of s[(y + 1) mod 2]
exit repeat
end if
end repeat
repeat with x=l to (r - 1)
put "X" into char x of s[y]
end repeat
add 1 to ans
put r into l
end repeat #end of l=1 to n
end repeat #end of y=0 to 1
repeat with x=1 to n
if (char x of s[0] is "." ) OR (char x of s[1] is "." ) then add 1 to ans
end repeat
return ans
end solve
#####END OF CODE#####
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w

MaxV
Posts: 1507
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: C++ code

Post by MaxV » Thu Jan 12, 2017 11:03 am

Just to give you a picture, this input:

Code: Select all

....X..........XX..XX..X..............................X..X..X...X...............X.X..X.......X.........X.......X.......XX..XX.........X....X......X...X....XX......X.....X.X....................X...X....X..X...X.........XX..........X.....X.......X.X...............X...X.X.X.X..XX.....X.........XX............X....X......X..XX...XX.X..X...........X.X........X..X......X............XX.X........XX......XX...XX............X..X......X.X......X....X...XXX.XXXX.X...X.........X.....XX...X......XXXXXX....X......X..........X.X.X.XX.X..X.........X.....X..X.XX.X..........XXXX.................X...X..X.XXX............X.XX..X..X....X...X.....................X..XX..........X.XX.....XX...X.....X...XX.X..XX.X.........X.X..X...XX..X............X.....X....X.......X.....X..XX.....X........X............X...X..X.XX.....XX...
X..X.......X............X.........X..X.X..XX.....X...............X.............X.....X.....X........XX...X.X......X...........X.X.X.....X.X.............X...X......X......X.X.X..X....X....X......X.XX.............X..X.XX....X........XXX...........X.X.X...XX.......XX......XX..X.X.X................X.X..........XX....X......XX.X.....X..X..............X.X...........XXXX...X.....X.......X.XX....X......X...X.XX..X...X..X.X.X.X........X........X.......X...X........X..XX........X........X...X....X.....X........X........X........X...................X.X....XX....XX.XX.XX.XX...XXX.......X.X...X.X...X............XX.X..XX..........X....X..X....X...........X.X.........X........X....XX.....X.XXX..............X.X....X.X...X......X.....X..XXXX...XX.XX...X..X......XXXX.X...X.....................X...........X.XX.X....
it has solution 222 towers (rooks).
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w

Post Reply

Return to “Converting to LiveCode”